你的位置:首页 > 操作系统

[操作系统]Android中Dialog对话框


布局文件复制代码

 1 <LinearLayout 复制代码

Activity文件:

普通的dialog:

复制代码
 1 private void plainDialogDemo() { 2 3     Button plainBtn = (Button) findViewById(R.id.plainDialog); 4     plainBtn.setOnClickListener(new OnClickListener() { 5 6       public void onClick(View v) { 7 8         new AlertDialog.Builder(DialogActivity.this) 9             .setTitle("删除")10             .setMessage("确定删除指定数据")11             .setPositiveButton("确定",12                 new DialogInterface.OnClickListener() {13 14                   @Override15                   public void onClick(DialogInterface dialog,16                       int which) {17                     Toast.makeText(getApplicationContext(),18                         "确定了", Toast.LENGTH_SHORT)19                         .show();20                   }21                 })22             .setNegativeButton("取消",23                 new DialogInterface.OnClickListener() {24 25                   @Override26                   public void onClick(DialogInterface dialog,27                       int which) {28                   }29                 }).setCancelable(false).show();30       }31     });32   }
复制代码

效果如下:

输入文本框的dialog:

复制代码
 1 private void inputDialog() { 2     Button inputBtn = (Button) findViewById(R.id.inputDialog); 3     inputBtn.setOnClickListener(new OnClickListener() { 4 5       @Override 6       public void onClick(View v) { 7         // TODO Auto-generated method stub 8         final EditText et = new EditText(DialogActivity.this); 9         new AlertDialog.Builder(DialogActivity.this)10             .setTitle("请输入数字")11             .setView(et)12             .setPositiveButton("确定",13                 new DialogInterface.OnClickListener() {14 15                   @Override16                   public void onClick(DialogInterface dialog,17                       int which) {18                     // TODO Auto-generated method stub19                     Toast.makeText(getApplicationContext(),20                         et.getText(),21                         Toast.LENGTH_SHORT).show();22                   }23                 }).setNegativeButton("取消", null)24             .setCancelable(false).show();25       }26     });27   }
复制代码

效果如下:

列表dialog:

复制代码
private void listDialogDemo() {    Button listBtn = (Button) findViewById(R.id.listDialog);    listBtn.setOnClickListener(new OnClickListener() {      @Override      public void onClick(View v) {        final String[] names = { "C罗", "J罗", "H罗" };        new AlertDialog.Builder(DialogActivity.this).setTitle("列表对话框")            .setItems(names, new DialogInterface.OnClickListener() {              @Override              public void onClick(DialogInterface dialog,                  int which) {                Toast.makeText(DialogActivity.this,                    names[which], Toast.LENGTH_SHORT)                    .show();              }            }).setNegativeButton("取消", null).show();      }    });  }
复制代码

效果如下:

单选dialog:

复制代码
 1 private void radioDialogDemo() { 2     Button radioButton = (Button) findViewById(R.id.radioDialog); 3     radioButton.setOnClickListener(new OnClickListener() { 4 5       @Override 6       public void onClick(View v) { 7 8         final String[] names = { "C罗", "J罗", "H罗" }; 9         new AlertDialog.Builder(DialogActivity.this)10             .setTitle("列表对话框")11             .setSingleChoiceItems(names, 0,12                 new DialogInterface.OnClickListener() {13 14                   @Override15                   public void onClick(DialogInterface dialog,16                       int which) {17 18                     selecteName = names[which];19                   }20                 })21             .setPositiveButton("确定",22                 new DialogInterface.OnClickListener() {23 24                   @Override25                   public void onClick(DialogInterface dialog,26                       int which) {27 28                     Toast.makeText(DialogActivity.this,29                         selecteName, Toast.LENGTH_SHORT)30                         .show();31                   }32                 }).setNegativeButton("取消", null).show();33       }34     });35   }
复制代码

效果如下:

多选dialog:

复制代码
 1 private void checkDialogDemo() { 2     Button checkBtn = (Button) findViewById(R.id.checkboxDialog); 3     checkBtn.setOnClickListener(new OnClickListener() { 4 5       @Override 6       public void onClick(View v) { 7         final String[] names = { "C罗", "J罗", "H罗" }; 8         final boolean[] selected = new boolean[] { true, false, true }; 9         new AlertDialog.Builder(DialogActivity.this)10             .setMultiChoiceItems(11                 names,12                 selected,13                 new DialogInterface.OnMultiChoiceClickListener() {14 15                   @Override16                   public void onClick(DialogInterface dialog,17                       int which, boolean isChecked) {18 19                   }20                 })21             .setPositiveButton("确定",22                 new DialogInterface.OnClickListener() {23 24                   @Override25                   public void onClick(DialogInterface dialog,26                       int which) {27                     StringBuilder sb = new StringBuilder(28                         "你选择了:");29                     for (int i = 0; i < names.length; i++) {30                       if (selected[i]) {31                         sb.append(names[i]);32                       }33                     }34                     Toast.makeText(DialogActivity.this,35                         sb.toString(), 1).show();36                   }37                 }).setNegativeButton("取消", null).show();38       }39     });40   }
复制代码

效果如下:

自定义dialog:

复制代码
 1 private void customDialogDemo() { 2     final AlertDialog dlg = new AlertDialog.Builder(this).create(); 3     dlg.show(); 4     Window window = dlg.getWindow(); 5     window.setContentView(R.layout.diylayout); 6     ImageButton ok = (ImageButton) window.findViewById(R.id.btnok); 7     ok.setOnClickListener(new View.OnClickListener() { 8 9       @Override10       public void onClick(View v) {11         Toast.makeText(getApplicationContext(), "关闭了",12             Toast.LENGTH_SHORT).show();13         dlg.dismiss();14       }15     });16   }
复制代码

自定义布局:

复制代码
 1 <?复制代码

效果如下: