お世話になっております。
RadioButtonを押した際に、値を変えていいかの確認メッセージを出し、
OKだったらチェックをつける、
キャンセルだったら何もしない、
という操作をしたいのですが、うまくできませんでした。
もしよい方法があれば教えて頂けませんでしょうか。
よろしくお願い致します。
以下、RadioButtonのonClickでダイアログを出して試したもの。
すでにチェック状態が変わってから確認をしている(?)ので
変更前の状態が保持できずにうまく動作しませんでした。
------------------------------------------------------------
public class RadioButtonTestActivity extends Activity {
RadioButton mRadioButton1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRadioButton1 = (RadioButton) findViewById(R.id.radioButton1);
mRadioButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(RadioButtonTestActivity.this)
.setMessage("値を変更してもいいですか?")
.setTitle("確認")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("キャンセル", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
mRadioButton1.setChecked(false);
}
})
.show();
}
});
}
}
------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/
android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton3" />
</RadioGroup>
</LinearLayout>
------------------------------------------------------------