Android:checkbox.isChecked()始终返回false条件

时间:2011-10-07 20:21:31

标签: android checkbox ischecked

我正在尝试将一个复选框编码到帮助屏幕中,该帮助屏幕实际上是一个弹出视图(由主程序启动的活动),其中包含带有帮助文本的ScrollView,以及确定按钮和一个要求的复选框如果您希望帮助屏幕在程序启动时自动显示。一切都在屏幕上正确显示,触摸时复选框切换。但是,当按下OK时,我用.isChecked()测试复选框的状态我总是得到一个假。我确信这是我错过的一些简单的事情。 XML文件如下:

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();
}
}

在“mDisplayHelp”行中设置断点始终在“false”分支处断开,无论复选框是否在按下“确定”按钮时显示绿色检查。

提前致谢。

编辑(10/10):

清楚我想要做的是在用户退出对话框后获取信息,这样我就可以感知复选框的状态并将其存储为首选项。为此我假设我必须在onDestory上测试它。所以,我这样做了:

@Override
public void onDestroy() {
    Boolean mDisplayHelp;

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

}

但是,无论选中或关闭复选框,我仍然总是会出现FALSE。在这种情况下,如果我不包含setContentView,我会在isChecked上获得NullPointerException。

2 个答案:

答案 0 :(得分:3)

为什么第二次调用setContentView?在您第二次调用它之后,您的复选框将被重置为未选中(默认状态),然后您将立即检查其状态以设置标志。

通过将helpdialog.xml两次膨胀,一次在Dialog中,一次在主Activity中,不清楚你的意图是什么。听起来你想在这里使用一个以对话为主题的活动,并且只在onCreate中调用一次setContentView()。之后它应该按预期运行。

答案 1 :(得分:1)

我终于想到了这一点,而男孩则以错误的方式处理事情。把它归结为学习曲线。

我需要做的是保存复选框状态。我需要使用SharedPreferences。我需要通过在其上使用监听器来检测何时触摸复选框,然后在那时保存首选项。打开帮助框后,我加载首选项状态并将复选框设置为该状态,以便根据用户以前的设置正确显示。

最后,在主要活动的onCreate中加载首选项,如果设置了首选项,则调用showHelpBox()。

on onCreate of main activity:

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

在帮助框活动中,根据以前的设置预先初始化复选框:

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

触摸复选框时:

    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();
        }
    });

希望其他人可以从我的愚蠢错误和误解中学习。

相关问题