给Button设置OnClick事件设置
int id=0;
final String [] s={"单选A","单选B","单选C","单选D","单选E"};
AlertDialog.Builder DanItem = new AlertDialog.Builder(MainActivity.this);
DanItem.setTitle("单选列表");
DanItem.setSingleChoiceItems(s, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ShowMessage(s[which].toString());
id=which;
}
});
DanItem.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ShowMessage("恭喜你选择了"+s[id].toString());
}
});
DanItem.create().show();
DanItem.setSingleChoiceItems第一个参数为(列表内容(建立的数组),默认选项为第几个,new一个Click事件)
如果选择了就给id赋予当前选中的下标
DanItem.setPositiveButton(按钮的标题,new一个Click事件)
调用展示方法
private void ShowMessage(String str) {
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}
创造展示
点击确定以后