博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Dialog的使用例子Demo
阅读量:6296 次
发布时间:2019-06-22

本文共 5903 字,大约阅读时间需要 19 分钟。

第一个XML,布局的

View Code
1 
10 11
17
24
31
38

第二个XML,实现第三个按钮的

View Code
1 
2
7 8
17
26
35
45

Activity

View Code
1 package com.example.clicktalk;  2   3 import android.os.Bundle;  4 import android.app.Activity;  5 import android.app.AlertDialog;  6 import android.app.AlertDialog.Builder;  7 import android.app.Dialog;  8 import android.app.ProgressDialog;  9 import android.content.Context; 10 import android.content.DialogInterface; 11 import android.view.LayoutInflater; 12 import android.view.Menu; 13 import android.view.View; 14 import android.widget.Button; 15 import android.widget.Toast; 16 import android.widget.ToggleButton; 17  18 public class MainActivity extends Activity { 19     //按钮1 20     private static final int dialog1 = 1; 21     private Button button01; 22     //按钮2 23     private static final int dialog2 = 2; 24     private Button button02; 25     //按钮3 26     private static final int dialog3 = 3; 27     private Button button03; 28     //按钮4 29     private static final int dialog4 = 4; 30     private Button button04; 31     @Override 32     protected void onCreate(Bundle savedInstanceState) { 33         super.onCreate(savedInstanceState); 34         setContentView(R.layout.activity_main); 35         //绑定监听按钮1 36         button01 = (Button)findViewById(R.id.MyButtonClick);     37         button01.setOnClickListener(new Button.OnClickListener(){ 38             @Override 39             public void onClick(View v) { 40                 showDialog(dialog1); 41                 // TODO Auto-generated method stub 42                 //Toast.makeText(MainActivity.this, "iu", Toast.LENGTH_LONG).show();             43             } 44              45         }); 46         //绑定监听按钮2 47         button02 = (Button)findViewById(R.id.MyButtonClick02); 48         button02.setOnClickListener(new Button.OnClickListener(){ 49             @Override 50             public void onClick(View v) { 51                 // TODO Auto-generated method stub 52                 showDialog(dialog2); 53             }     54         }); 55         //绑定监听按钮3 56         button03 = (Button)findViewById(R.id.MyButtonClick03); 57         button03.setOnClickListener(new Button.OnClickListener(){ 58  59             @Override 60             public void onClick(View v) { 61                 // TODO Auto-generated method stub 62                 showDialog(dialog3); 63             } 64         }); 65         //绑定监听按钮4 66         button04 = (Button)findViewById(R.id.MyButtonClick04); 67         button04.setOnClickListener(new Button.OnClickListener(){ 68  69             @Override 70             public void onClick(View v) { 71                 // TODO Auto-generated method stub 72                 showDialog(dialog4); 73             } 74         });         75     } 76  77     protected Dialog onCreateDialog(int id) { 78         switch (id) { 79         case dialog1:             80             return buildDialog1(MainActivity.this);     81         case dialog2: 82             return buildDialog2(MainActivity.this); 83         case dialog3: 84             return buildDialog3(MainActivity.this); 85         case dialog4: 86             return buildDialog4(MainActivity.this); 87         } 88         return null; 89     } 90      91     private Dialog buildDialog1(Context context) { 92         AlertDialog.Builder builder = new AlertDialog.Builder(context); 93         builder.setTitle("who are you?"); 94         builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 95              96             @Override 97             public void onClick(DialogInterface dialog, int which) { 98                 // TODO Auto-generated method stub 99                 Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();100             }101         });102         builder.setNegativeButton("No", new DialogInterface.OnClickListener() {103             104             @Override105             public void onClick(DialogInterface dialog, int which) {106                 // TODO Auto-generated method stub107                 Toast.makeText(MainActivity.this, "取消按钮", Toast.LENGTH_SHORT).show();//在第一个参数中用this显示makeText红线,用MainActivity.this表本身即可108             }109         });        110         return builder.create();111     }112     private Dialog buildDialog2(Context context) {113         AlertDialog.Builder builder = new AlertDialog.Builder(context);114         builder.setTitle("button02");115         builder.setMessage("who are you?");116         builder.setPositiveButton("Yes", null);117         builder.setNegativeButton("No", null);118         return builder.create();119     }120     private Dialog buildDialog3(Context context) {        121         LayoutInflater inflater = LayoutInflater.from(this);122         final View textEntryView = inflater.inflate(R.layout.alert_dialog_text_entry, null);123         124         AlertDialog.Builder builder = new AlertDialog.Builder(context);125         builder.setTitle("Hello");126         builder.setView(textEntryView);//关键127         builder.setPositiveButton("YES", null);128         builder.setNegativeButton("NO", null);        129         return builder.create();    130     }131     private Dialog buildDialog4(Context context) {132         ProgressDialog dialog = new ProgressDialog(context);133         dialog.setTitle("waiting");134         dialog.setMessage("loading");135         return dialog;136     }    137     public boolean onCreateOptionsMenu(Menu menu) {138         // Inflate the menu; this adds items to the action bar if it is present.139         getMenuInflater().inflate(R.menu.main, menu);140         return true;141     }142 143 }

参照

记下代码,方便以后查看

转载于:https://www.cnblogs.com/newlooker/archive/2013/04/08/3007479.html

你可能感兴趣的文章
在git@osc上托管自己的代码
查看>>
机器学习算法:朴素贝叶斯
查看>>
小五思科技术学习笔记之扩展访问列表
查看>>
使用Python脚本检验文件系统数据完整性
查看>>
使用MDT部署Windows Server 2003 R2
查看>>
Redhat as5安装Mysql5.0.28
查看>>
通过TMG发布ActiveSync
查看>>
Web服务器的配置与管理(4) 配置访问权限和安全
查看>>
sql注入之order by猜列数问题
查看>>
将域用户加入本地power user组的脚本
查看>>
python range()内建函数
查看>>
Git 远程分支的pull与push
查看>>
React源码学习——ReactClass
查看>>
电脑爱好者GHOSTWIN764位V4.0
查看>>
MYSQL——常用运算符和函数
查看>>
JS获取上传文件的大小
查看>>
Lync Server 2010迁移至Lync Server 2013故障排错Part1:缺少McsStandalone.msi
查看>>
域控制器建立教程
查看>>
RHCE 学习笔记(20) ACL
查看>>
Django 和 Ajax 简介
查看>>