チェックボックスをヘルプ画面にコード化しようとしています。これは基本的にポップアップ ビュー (メイン プログラムによって開始されるアクティビティ) であり、ヘルプ テキストと [OK] ボタンを含む ScrollView と、必要かどうかを尋ねるチェックボックスが含まれています。プログラムの開始時にヘルプ画面が自動的に表示されるようにします。すべてが画面に正しく表示され、タッチするとチェックボックスが切り替わります。ただし、[OK] を押すと、チェックボックスの状態を .isChecked() でテストすると、常に false になります。私が見逃したのは単純なことだと確信しています。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」行にブレークポイントを設定すると、[OK] ボタンが押されたときにチェック ボックスに緑色のチェックが表示されるかどうかに関係なく、常に「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 が発生します。