android_checkbox_dialog设计是不是开起默认不提示
package com.example.android_checkbox_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;
//定义复选框选项
final String[] multiChoiceItems = {"开启默认不提示"};
//复选框默认值:false=未选;true=选中 ,各自对应items[i]
final boolean[] defaultSelectedStatus = {false};
@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) {
if(defaultSelectedStatus[0] != true)
showDialog5();
else
Toast.makeText(context,"已经开启默认设置", Toast.LENGTH_LONG).show();
}
});
}
public void showDialog5(){
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();
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-03 15:48:34