我有以下alertdialog代码:
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setTitle("Options");
helpBuilder.setMessage("Choose Your Option");
LayoutInflater inflater = getLayoutInflater();
View radioButtonLayout = inflater.inflate(R.layout.popuplayout, null);
byNameRadioButton = (RadioButton) findViewById(R.id.byname);
byIdRadioButton = (RadioButton) findViewById(R.id.byid);
helpBuilder.setView(radioButtonLayout);
helpBuilder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Tab1Activity.this,
"Ok button is clicked", Toast.LENGTH_LONG)
.show();
}
}
});
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
我想知道点击确定按钮时点击了哪个单选按钮? 需要帮助。 Thanx!
答案 0 :(得分:3)
为什么不使用带有单选按钮的Dialog的默认实现,您可以在此处找到:Dialogs。您不需要编写任何额外的代码,您将获得所需的! :) 这是一个例子:
final CharSequence[] items = {"By Score", "By Name", "By Id"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Options");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();