packagecom.ethan.dialog;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.Context;
importandroid.content.DialogInterface;
importandroid.content.DialogInterface.OnMultiChoiceClickListener;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
importandroid.widget.Toast;
publicclass TestDialogActivity extendsActivity {
privateButton btn;
finalContext context = this;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(newView.OnClickListener() {
@Override
publicvoid onClick(View v) {
showDialog5();
}
});
}
publicvoid showDialog5(){
//定义复选框选项
finalString[] multiChoiceItems = {"Item1","Item2"};
//复选框默认值:false=未选;true=选中 ,各自对应items[i]
finalboolean[] defaultSelectedStatus = {false,false};
finalStringBuilder sb = newStringBuilder();
//创建对话框
newAlertDialog.Builder(context)
.setTitle("复选框")//设置对话框标题
.setMultiChoiceItems(multiChoiceItems, defaultSelectedStatus, newOnMultiChoiceClickListener(){
@Override
publicvoid onClick(DialogInterface dialog, intwhich,
booleanisChecked) {
//来回重复选择取消,得相应去改变item对应的bool值,点击确定时,根据这个bool[],得到选择的内容
defaultSelectedStatus[which] = isChecked;
}
}) //设置对话框[肯定]按钮
.setPositiveButton("确定",newDialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, intwhich) {
for(inti=0;i<defaultSelectedStatus.length;i++) {
if(defaultSelectedStatus[i]) {
sb.append(multiChoiceItems[i]);
}
}
// TODO Auto-generated method stub
Toast.makeText(context,sb.toString(), Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消",null)//设置对话框[否定]按钮
.show();
}
}
Android 之 Dialog复选框获取值
最新推荐文章于 2021-05-28 20:40:04 发布