1 packagecom.example.lesson7_3_id19_alertdialog;2
3 importandroid.app.AlertDialog;4 importandroid.content.DialogInterface;5 importandroid.os.Bundle;6 importandroid.support.v7.app.AppCompatActivity;7 importandroid.view.View;8 importandroid.widget.ArrayAdapter;9 importandroid.widget.EditText;10 importandroid.widget.Toast;11
12 public class MainActivity extendsAppCompatActivity {13
14 @Override15 protected voidonCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.activity_main);18 }19 public voiddialog_1(View v){20 AlertDialog.Builder builder = new AlertDialog.Builder(this);21 builder.setIcon(R.mipmap.ic_launcher_round);22
23 builder.setTitle("标题栏");24 builder.setMessage("正文部分,简单的文本");25 builder.setPositiveButton("确定",newDialogInterface.OnClickListener() {26 @Override27 public void onClick(DialogInterface dialog, intwhich) {28 Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();29 }30 });31 builder.setNegativeButton("取消",null);32 builder.setNeutralButton("中立",null);33 AlertDialog alertDialog =builder.create();34 alertDialog.show();35
36 }37 private String [] item = {"游戏","运动","电影","旅游","看书"};38 public voiddialog_2(View v){39 AlertDialog.Builder builder = new AlertDialog.Builder(this);40 builder.setTitle("请选择");41 builder.setItems(item, newDialogInterface.OnClickListener() {42 @Override43 public void onClick(DialogInterface dialog, intwhich) {44 Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();45 }46 });47 //取消可以不添加48 //builder.setNegativeButton("取消",null);
49 AlertDialog alertDialog =builder.create();50 alertDialog.show();51
52 }53 intindex;54 public voiddialog_3(View v){55 AlertDialog.Builder builder = new AlertDialog.Builder(this);56 builder.setTitle("请选择");57 builder.setSingleChoiceItems(item, index, newDialogInterface.OnClickListener() {58 @Override59 public void onClick(DialogInterface dialog, intwhich) {60 index =which;61 }62 });63 builder.setPositiveButton("确定", newDialogInterface.OnClickListener() {64 @Override65 public void onClick(DialogInterface dialog, intwhich) {66 Toast.makeText(MainActivity.this, "选择了"+item[index], Toast.LENGTH_SHORT).show();67 }68 });69 builder.setNegativeButton("取消",null);70 AlertDialog alertDialog =builder.create();71 alertDialog.show();72 }73 //设置boolean数组所有的选项设置默认没选
74 boolean[] bools = {false,false,false,false,false};75 public voiddialog_4(View v){76 AlertDialog.Builder builder = new AlertDialog.Builder(this);77 builder.setTitle("请选择");78 builder.setMultiChoiceItems(item, bools, newDialogInterface.OnMultiChoiceClickListener() {79 @Override80 public void onClick(DialogInterface dialog, int which, booleanisChecked) {81 bools[which] =isChecked;82 }83 });84 builder.setPositiveButton("确定", newDialogInterface.OnClickListener() {85 @Override86 public void onClick(DialogInterface dialog, intwhich) {87 StringBuffer sb = newStringBuffer();88 for (int i = 0; i < item.length; i++) {89 if(bools[i]) {90 sb.append(item[i] + " ");91 }92 }93 Toast.makeText(MainActivity.this, "选择了" +sb.toString(), Toast.LENGTH_SHORT).show();94 }95 });96 builder.setNegativeButton("取消",null);97 AlertDialog alertDialog =builder.create();98 alertDialog.show();99 }100 public voiddialog_5(View v){101 AlertDialog.Builder builder = new AlertDialog.Builder(this);102 builder.setTitle("连接wifi");103 final EditText et = new EditText(this);104 et.setHint("请输入密码");105 et.setSingleLine(true);106 builder.setView(et);107 builder.setNegativeButton("取消",null);108 builder.setPositiveButton("确定", newDialogInterface.OnClickListener() {109 @Override110 public void onClick(DialogInterface dialog, intwhich) {111 String password =et.getText().toString();112 if (password.equals("123456")) {113 Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();114 }else{115 Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_SHORT).show();116 }117 }118 });119 AlertDialog alertDialog =builder.create();120 alertDialog.show();121
122 }123 public voiddialog_6(View v){124 ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,item);125 AlertDialog.Builder builder = new AlertDialog.Builder(this);126 builder.setTitle("使用适配器");127 builder.setAdapter(adapter, newDialogInterface.OnClickListener() {128 @Override129 public void onClick(DialogInterface dialog, intwhich) {130 Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();131 }132 });133 AlertDialog alertDialog =builder.create();134 alertDialog.show();135 }136 }