如何在使用AlertDialog时获取TextView 中复选框的状态。
我仍然试图显示CheckBox 的 Checked 状态,但没有得到任何值,无论是 True 还是 False。
请看下面的截图,
UploadActivity.java 代码:
TextView tv1;
CheckBox chkIos;
@Override
public Dialog onCreateDialog(int id) {
AlertDialog dialogDetails = null;
switch (id) {
case DIALOG_LOGIN:
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
dialogbuilder.setTitle("Image Information");
dialogbuilder.setView(dialogview);
dialogDetails = dialogbuilder.create();
break;
}
return dialogDetails;
}
@Override
public void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_LOGIN:
final AlertDialog alertDialog = (AlertDialog) dialog;
Button loginbutton = (Button) alertDialog
.findViewById(R.id.btn_login);
Button cancelbutton = (Button) alertDialog
.findViewById(R.id.btn_cancel);
tv1 = (TextView) alertDialog
.findViewById(R.id.textView1);
chkIos = (CheckBox) alertDialog
.findViewById(R.id.chkOption1);
loginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SaveData();
addListenerOnButton();
}
private boolean SaveData() {
.............
}
return true;
}
private String getHttpPost(String url,
List<NameValuePair> params) {
.............
}
return str.toString();
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...........
}
});
}
}
public void addListenerOnButton() {
chkIos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked())
tv1.setText("True");
else
tv1.setText("False");
}
});
}