实现对话框这里是我的对话框与复选框
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button no;
CheckBox yes;
boolean p;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
yes = (CheckBox) findViewById(R.id.checkBox1);
no = (Button) findViewById(R.id.btn_no);
no.setOnClickListener(this);
yes.setChecked(false);
yes.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.checkBox1:
yes.setChecked(true);
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
}
}
现在,我打开对话框,选中该复选框,单击按钮,关闭对话框。但是当我再次打开它时,复选框又被取消选中。我该怎么办?
的提示是的onCreate(捆绑savedInstanceState)给出。您需要保存复选框值,然后将其传回以从savedInstanceState包中检索它们。 –
现在不好使用Dialog。强烈建议使用DialogFragment方法。 showDialog()从API级别13开始被弃用。请参阅下面的答案。 – a11n