Android:checkbox.isChecked() 总是返回错误条件
我正在尝试将一个复选框编码到帮助屏幕中,该屏幕本质上是一个弹出视图(由主程序启动的活动),其中包含一个带有帮助文本的 ScrollView、“确定”按钮和一个询问您是否想要的复选框帮助屏幕在程序启动时自动出现。所有内容都正确显示在屏幕上,并且触摸时复选框会切换。但是,当按下“确定”并使用 .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。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么要第二次调用 setContentView ?第二次调用它后,您的复选框将被重置为未选中(默认状态),然后您立即检查其状态以设置标志。
目前尚不清楚您通过两次膨胀 helpdialog.xml 的意图是什么,一次在对话框中,一次在主活动中。听起来您想在这里使用以对话框为主题的活动,并且只在 onCreate 中调用一次 setContentView() 。之后它应该按预期运行。
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.
我终于明白了这一点,天哪,我处理事情的方式是错误的。将其归结为学习曲线。
我需要做的是保存复选框状态。我需要为此使用 SharedPreferences 。我需要使用侦听器来检测复选框何时被触摸,然后保存当时的首选项。打开帮助框后,我加载首选项状态并将复选框设置为该状态,以便它根据用户之前的设置正确显示。
最后,在主活动的 onCreate 中加载首选项,如果已设置首选项,则调用 showHelpBox()。
在主要活动的 onCreate 中:
在帮助框活动中,根据之前的设置预初始化复选框:
当复选框被触摸时:
希望其他人能够从我的愚蠢错误和误解中吸取教训。
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:
In help box activity pre-initialize the check box based on previous setting:
When checkbox is touched:
Hopefully others can learn from my foolish mistakes and misunderstanding.