PHP
·
发表于 5年以前
·
阅读量:8410
package com.ethan.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TestDialogActivity extends Activity {
private Button btn;
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog5();
}
});
}
public void showDialog5(){
//定义复选框选项
final String[] multiChoiceItems = {"Item1","Item2"};
//复选框默认值:false=未选;true=选中 ,各自对应items[i]
final boolean[] defaultSelectedStatus = {false,false};
final StringBuilder sb = new StringBuilder();
//创建对话框
new AlertDialog.Builder(context)
.setTitle("复选框")//设置对话框标题
.setMultiChoiceItems(multiChoiceItems, defaultSelectedStatus, new OnMultiChoiceClickListener(){
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
//来回重复选择取消,得相应去改变item对应的bool值,点击确定时,根据这个bool[],得到选择的内容
defaultSelectedStatus[which] = isChecked;
}
}) //设置对话框[肯定]按钮
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i=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();
}
}