开发者

Android: checkbox.isChecked() is always returning a false condition

开发者 https://www.devze.com 2023-04-12 01:46 出处:网络
I\'m trying to code a checkbox into a help screen which is essentially a pop up view (an activity that\'s started by the main program) containing a ScrollView that has the help text, and OK button, an

I'm trying to code a checkbox into a help screen which is essentially a pop up view (an activity that's started by the main program) containing a ScrollView that has the help text, and OK button, and a checkbox that asks if you want the help screen to appear automatically at program start. Everything displays properly on the screen, and the checkbox toggles, when touched. However, when OK is pressed, and I test the state of the checkbox with .isChecked() I'm always getting a false. I'm sure it's something simple I've missed. XML file follows follows:

helpdialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent" android:layout_height="wrap_content"
 android:background="#ffffff">

 <ScrollView android:id="@+id/ScrollView01"
 android:layout_width="wrap_content" android:layout_below="@+id/ImageView01"
 android:layout_height="300px">

 <TextView android:text="@+id/helpView" android:id="@+id/helpView"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:textColor="#000000"/>

</ScrollView>
<Button android:id="@+id/Button01" 
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true" android:text="  OK  "
android:layout_below="@id/ScrollView01"/>

<CheckBox android:id="@+id/chkNoHelp" android:textColor="#000000"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:text="Don't Display at Startup"
android:layout_below="@id/Button01"        />  

</RelativeLayout>

HelpBox.java:

public class HelpBox extends Activity {

CheckBox checkBox;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     //set up dialog
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.helpdialog);
    dialog.setTitle("Help");
   开发者_运维百科 dialog.setCancelable(true);

    //set up text
    TextView text = (TextView) dialog.findViewById(R.id.helpView);
    text.setText(getString(R.string.help_text));

    //set up button
    Button button = (Button) dialog.findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Boolean mDisplayHelp;

            setContentView(R.layout.helpdialog);

            checkBox = (CheckBox) findViewById(R.id.chkNoHelp);
            if (checkBox.isChecked()) {
                mDisplayHelp = true;
            } else {
                mDisplayHelp = false;
            }
            finish();
        }
    });
    //now that the dialog is set up, it's time to show it    
    dialog.show();
}
}

Setting breakpoints on both "mDisplayHelp" lines always breaks at the 'false' branch regardless of whether the check box displays a green check or not when the OK button is pressed.

Thanks in advance.

Edit (10/10):

Its clear what I want to do is pick up information after the user exits the dialog, so I can sense the state of the checkbox and store it as a preference. For this I assume I have to test it in onDestory. So, I did that:

@Override
public void onDestroy() {
    Boolean mDisplayHelp;

    setContentView(R.layout.helpdialog);        
    checkBox = (CheckBox) findViewById(R.id.chkNoHelp);
    if (checkBox.isChecked()) {
        mDisplayHelp = true;
    } else {
        mDisplayHelp = false;
    }

}

However, I'm still always coming up with FALSE as a result, regardless of whether the checkbox is display checked or off. In this instance, if I don;t include the setContentView, I get a NullPointerException on the isChecked.


Why are you calling setContentView a second time? After the second time you call it, your checkbox is being reset to unchecked (the default state) and then you are immediately checking its state to set a flag.

Its not clear what your intention is here by inflating helpdialog.xml twice, once in a Dialog and once in your main Activity. It sounds like you want to use a dialog-themed activity here and only call setContentView() once in onCreate. It should behave as expected after that.


I finally figured this out, and boy was I going about things the wrong way. Chalk it up to learning curve.

What I needed to do was save the checkbox state. I needed to use SharedPreferences for that. I needed to detect when the checkbox was touched by using a listener on it and then saving the preference at that time. Upon opening the help box, I load the preference state and set the check box to that state so it shows properly based on the user's previous setting.

Finally, load the preference in onCreate at the main activity and call the showHelpBox() if the preference was so set.

In onCreate of main activity:

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    Boolean mDisplayHelp = prefs.getBoolean("checkboxPref", false);
    if (mDisplayHelp == false)
        showHelpBox();

In help box activity pre-initialize the check box based on previous setting:

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    mDisplayHelp = prefs.getBoolean("checkboxPref", false);
    final CheckBox ckBox = (CheckBox) dialog.findViewById(R.id.chkNoHelp);
    ckBox.setChecked(mDisplayHelp);

When checkbox is touched:

    ckBox.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

          if (ckBox.isChecked()) {
            mDisplayHelp = true;
          } else {
            mDisplayHelp = false;
          }
          SharedPreferences.Editor editor = prefs.edit();
          editor.putBoolean("checkboxPref", mDisplayHelp);

          // Don't forget to commit your edits!!!
          editor.commit();
        }
    });

Hopefully others can learn from my foolish mistakes and misunderstanding.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号