码迷,mamicode.com
首页 > 移动开发 > 详细

各种安卓对话框代码合集

时间:2016-05-14 16:57:36      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

1.代码运行目录

AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.administrator.android">
 4 
 5     <application
 6         android:allowBackup="true"
 7         android:icon="@mipmap/ic_launcher"
 8         android:label="@string/app_name"
 9         android:supportsRtl="true"
10         android:theme="@style/AppTheme">
11         <activity android:name=".MainActivity" />
12         <activity android:name=".TestActivity4" />
13         <activity android:name=".Main2Activity" />
14         <activity android:name=".Main3Activity">
15             <intent-filter>
16                 <action android:name="android.intent.action.MAIN" />
17                 <category android:name="android.intent.category.LAUNCHER" />
18             </intent-filter>
19         </activity>
20         <activity android:name=".Main5Activity"></activity>
21     </application>
22 
23 </manifest>

2.布局文件

activity_mani.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     tools:context="com.example.administrator.android.Main3Activity"
 7     android:orientation="vertical">
 8     <!--对话框-->
 9         <Button
10             android:layout_width="match_parent"
11             android:layout_height="wrap_content"
12             android:text="一般对话框"
13             android:onClick="bt1_onclick"/>
14         <Button
15             android:layout_width="match_parent"
16             android:layout_height="wrap_content"
17             android:text="单选对话框"
18             android:onClick="bt2_onclick"/>
19         <Button
20             android:layout_width="match_parent"
21             android:layout_height="wrap_content"
22             android:text="复选对话框"
23             android:onClick="bt3_onclick"/>
24         <Button
25             android:layout_width="match_parent"
26             android:layout_height="wrap_content"
27             android:text="自定义对话框"
28             android:onClick="bt4_onclick"/>
29         <Button
30             android:layout_width="match_parent"
31             android:layout_height="wrap_content"
32             android:text="登录对话框"
33             android:onClick="bt5_onclick"/>
34         <Button
35             android:layout_width="match_parent"
36             android:layout_height="wrap_content"
37             android:text="日期对话框"
38             android:onClick="bt6_onclick"/>
39         <Button
40             android:layout_width="match_parent"
41             android:layout_height="wrap_content"
42             android:text="时间对话框"
43             android:onClick="bt7_onclick"/>
44         <Button
45             android:layout_width="match_parent"
46             android:layout_height="wrap_content"
47             android:text="普通进度对话框"
48             android:onClick="bt8_onclick"/>
49 </LinearLayout>

截屏如下:

技术分享

3.各种对话框的运行JAVA代码。

Mani3Activity.java

第一种:一般对话框

 1 package com.example.administrator.android;
 2 
 3 import android.app.AlertDialog;
 4 import android.app.DatePickerDialog;
 5 import android.app.Dialog;
 6 import android.app.ProgressDialog;
 7 import android.app.TimePickerDialog;
 8 import android.content.DialogInterface;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.os.Bundle;
11 import android.text.Layout;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.DatePicker;
15 import android.widget.EditText;
16 import android.widget.TimePicker;
17 import android.widget.Toast;
18 import java.util.Calendar;
19 
20 public class Main3Activity extends AppCompatActivity {
21 
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main3);
27     }
28     public void bt1_onclick(View v) {
29         //一般对话框 不能直接实例化
30         //内部提供构造器
31         //方法链调用
32         AlertDialog alertDialog = new AlertDialog.Builder(this)
33                 .setTitle("确认对话框")
34                 .setMessage("您确实要删除吗?")
35                 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
36                     @Override
37                     public void onClick(DialogInterface dialog, int which) {
38                         Toast.makeText(Main3Activity.this, "取消删除……,which = " + which, Toast.LENGTH_SHORT).show();
39                     }
40                 })//负面按钮
41 
42                 .setPositiveButton("确认", new DialogInterface.OnClickListener() {
43                     @Override
44                     public void onClick(DialogInterface dialog, int which) {
45                         Toast.makeText(Main3Activity.this, "执行删除……,which = " + which, Toast.LENGTH_SHORT).show();
46                     }
47                 })//正面按钮
48 
49                 .setNeutralButton("中立", new DialogInterface.OnClickListener() {
50                     @Override
51                     public void onClick(DialogInterface dialog, int which) {
52                         Toast.makeText(Main3Activity.this, "执行命令……,which = " + which, Toast.LENGTH_SHORT).show();
53                     }
54                 })//普通按钮
55                 .setCancelable(false) //杜绝点击其他位置关闭对话框 不能以分号结尾
56                 .show(); //显示对话框的方法
57     }
58 }

第二种:单选对话框

技术分享
 1 package com.example.administrator.android;
 2 
 3 import android.app.AlertDialog;
 4 import android.app.DatePickerDialog;
 5 import android.app.Dialog;
 6 import android.app.ProgressDialog;
 7 import android.app.TimePickerDialog;
 8 import android.content.DialogInterface;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.os.Bundle;
11 import android.text.Layout;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.DatePicker;
15 import android.widget.EditText;
16 import android.widget.TimePicker;
17 import android.widget.Toast;
18 import java.util.Calendar;
19 
20 public class Main3Activity extends AppCompatActivity {
21 
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main3);
27     }
28  //单选对话框
29     public void bt2_onclick(View v) {
30         //final,中态 延长常量生命周期
31         final String[] str = {"男", "女"};
32         final AlertDialog a = new AlertDialog.Builder(this)
33                 .setTitle("单选对话框")
34                 .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
35                     @Override
36                     public void onClick(DialogInterface dialog, int which) {
37                         Toast.makeText(Main3Activity.this, "which = " + which +
38                                 ",选中的是:" + str[which], Toast.LENGTH_LONG).show();
39                         //关闭对话框
40                         dialog.dismiss();
41                     }
42                 })
43                 .setCancelable(false)  //模态窗口
44                 .show();
45     }
46 }
单选对话框

第三种:复选对话框

技术分享
 1 package com.example.administrator.android;
 2 
 3 import android.app.AlertDialog;
 4 import android.app.DatePickerDialog;
 5 import android.app.Dialog;
 6 import android.app.ProgressDialog;
 7 import android.app.TimePickerDialog;
 8 import android.content.DialogInterface;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.os.Bundle;
11 import android.text.Layout;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.DatePicker;
15 import android.widget.EditText;
16 import android.widget.TimePicker;
17 import android.widget.Toast;
18 import java.util.Calendar;
19 
20 public class Main3Activity extends AppCompatActivity {
21 
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main3);
27     }
28 //复选对话框
29     public void bt3_onclick(View v)
30     {
31         final String [] str = {"宝马","奔驰","劳斯莱斯","宾利"};
32         final boolean[] ch = {true,false,false,true};
33         new AlertDialog.Builder(this)
34                 .setTitle("复选对话框")
35                 .setMultiChoiceItems(str, ch, new DialogInterface.OnMultiChoiceClickListener() {
36                     @Override
37                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
38                         //改变对应数组项的选中值状态
39                         ch[which]=isChecked;
40                     }
41                 })
42                 //添加响应按钮
43                 .setPositiveButton("确认", new DialogInterface.OnClickListener() {
44                     @Override
45                     public void onClick(DialogInterface dialog, int which) {
46                         int i = 0;
47                         //获取最终的选择状态
48                         for (boolean b : ch) {
49                             Toast.makeText(Main3Activity.this, str[i] + "选中的输出状态=" +
50                                     (b ? "选中" : "未选中"), Toast.LENGTH_LONG).show();
51                             i++;
52                         }
53                     }
54                 })
55                 .setNegativeButton("取消", null)
56                 .setCancelable(false)
57                 .show();
58     }
59 }
复选对话框

第四种:自定义对话框

技术分享
 1 package com.example.administrator.android;
 2 
 3 import android.app.AlertDialog;
 4 import android.app.DatePickerDialog;
 5 import android.app.Dialog;
 6 import android.app.ProgressDialog;
 7 import android.app.TimePickerDialog;
 8 import android.content.DialogInterface;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.os.Bundle;
11 import android.text.Layout;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.DatePicker;
15 import android.widget.EditText;
16 import android.widget.TimePicker;
17 import android.widget.Toast;
18 import java.util.Calendar;
19 
20 public class Main3Activity extends AppCompatActivity {
21 
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main3);
27     }
28  //自定义对话框
29     public void bt4_onclick(View v)
30     {
31         LayoutInflater layoutInflater = getLayoutInflater();
32        View view =  layoutInflater.inflate(R.layout.dialoglayout, null);
33         new AlertDialog.Builder(this)
34                 .setTitle("自定义对话框")
35                 .setPositiveButton("确定", new DialogInterface.OnClickListener() {
36                     @Override
37                     public void onClick(DialogInterface dialog, int which) {
38                         Toast.makeText(Main3Activity.this, "执行结束", Toast.LENGTH_SHORT).show();
39                     }
40                 })
41                 .setView(view)
42                 .show();
43     }
44 }
自定义对话框

第五种:登录对话框

技术分享
 1 package com.example.administrator.android;
 2 
 3 import android.app.AlertDialog;
 4 import android.app.DatePickerDialog;
 5 import android.app.Dialog;
 6 import android.app.ProgressDialog;
 7 import android.app.TimePickerDialog;
 8 import android.content.DialogInterface;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.os.Bundle;
11 import android.text.Layout;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.DatePicker;
15 import android.widget.EditText;
16 import android.widget.TimePicker;
17 import android.widget.Toast;
18 import java.util.Calendar;
19 
20 public class Main3Activity extends AppCompatActivity {
21 
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main3);
27     }
28 //登录对话框
29     public void bt5_onclick(View v)
30     {
31         //加载Layout文件
32         final View view = View.inflate(this, R.layout.dialog_login, null);
33 
34         AlertDialog al = new AlertDialog.Builder(this)
35                 .setTitle("登录对话框")
36                 .setView(view)
37                 .setPositiveButton("登录", new DialogInterface.OnClickListener() {
38                     @Override
39                     public void onClick(DialogInterface dialog, int which) {
40                         //转换
41                         AlertDialog al = (AlertDialog)dialog;
42                         //获取输入信息
43                         EditText et_1 = (EditText)al.findViewById(R.id.et_1);
44 
45                         Toast.makeText(Main3Activity.this, "用户名 = "+
46                                 et_1.getText(), Toast.LENGTH_SHORT).show();
47                         //验证并提示
48                     }
49                 })
50                 .setNegativeButton("取消",null)
51                 .show();
52     }
53 }
登录对话框

第六种:日历选择对话框

技术分享
 1 package com.example.administrator.android;
 2 
 3 import android.app.AlertDialog;
 4 import android.app.DatePickerDialog;
 5 import android.app.Dialog;
 6 import android.app.ProgressDialog;
 7 import android.app.TimePickerDialog;
 8 import android.content.DialogInterface;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.os.Bundle;
11 import android.text.Layout;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.DatePicker;
15 import android.widget.EditText;
16 import android.widget.TimePicker;
17 import android.widget.Toast;
18 import java.util.Calendar;
19 
20 public class Main3Activity extends AppCompatActivity {
21 
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main3);
27     }
28  //当前日期 日历
29     //单例模式
30     Calendar c = Calendar.getInstance();
31     //日期对话框
32     public void bt6_onclick(View v)
33     {
34         DatePickerDialog dp = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
35             @Override
36             public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
37                 Toast.makeText(Main3Activity.this, year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
38             }
39         }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
40 
41         dp.setCancelable(false);
42 
43         dp.show();
44     }
45 }
日历选择对话框

第七种:时间选择对话框

技术分享
 1 package com.example.administrator.android;
 2 
 3 import android.app.AlertDialog;
 4 import android.app.DatePickerDialog;
 5 import android.app.Dialog;
 6 import android.app.ProgressDialog;
 7 import android.app.TimePickerDialog;
 8 import android.content.DialogInterface;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.os.Bundle;
11 import android.text.Layout;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.widget.DatePicker;
15 import android.widget.EditText;
16 import android.widget.TimePicker;
17 import android.widget.Toast;
18 import java.util.Calendar;
19 
20 public class Main3Activity extends AppCompatActivity {
21 
22 
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main3);
27     }
28 //时间对话框
29     public void bt7_onclick(View v)
30     {
31         TimePickerDialog tp = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
32             @Override
33             public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
34                 Toast.makeText(Main3Activity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
35             }
36         }, c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE), true);
37 
38         tp.setCanceledOnTouchOutside(false);
39 
40         tp.show();
41     }
42 }
时间选择对话框

第八种:普通进度对话框

技术分享
 1  //普通进度对话框
 2     public void bt8_onclick(View v)
 3     {
 4         final ProgressDialog progressDialog = new ProgressDialog(this);
 5         progressDialog.setTitle("进度对话框");
 6         progressDialog.setMessage("请等待. . .");
 7         progressDialog.setCancelable(false);
 8         progressDialog.show();
 9         //不要写业务代码
10         //开启子线程 实现多线程
11         //1.继承 2.实现接口
12         //1.继承 Thread,重写run方法,调用start()
13         new Thread(){
14             @Override
15             public void run() {
16                 try {
17                     //  延迟 线程
18                     Thread.sleep(3000);
19                 }
20                 catch (Exception e)
21                 {
22                 }
23                 progressDialog.dismiss();//关闭方法
24             }
25         }.start();//start负责启动子线程
26     }
普通进度对话框

第九种:水平进度对话框

技术分享
 1 //水平进度对话框
 2     public void bt9_onclick(View v)
 3     {
 4         final ProgressDialog progressDialog = new ProgressDialog(this);
 5         progressDialog.setTitle("水平进度对话框");
 6         progressDialog.setMessage("正在加载 . . .");
 7         progressDialog.setCancelable(false);
 8         //设置水平
 9         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
10         progressDialog.show();
11         final TextView tv_2 = (TextView)findViewById(R.id.tv_2);
12         //启动子线程-实现接口方式Runnable启动
13         //匿名内部类
14         new Thread(new Runnable() {
15             @Override
16             public void run() {
17             //模拟进度变化
18                 for (int i = 0 ; i <= 100;i++) {
19                     progressDialog.setProgress(i);
20                     try {
21                         //  延迟 线程
22                         Thread.sleep(200);
23                     } catch (Exception e) {
24                     }
25                 }
26                 //在子线程里访问UI线程的视图
27                 runOnUiThread(new Runnable() {
28                     @Override
29                     public void run() {
30                         tv_2.setText("下载完成!");
31                     }
32                 });
33                 //执行完业务代码
34                 //关闭
35                 progressDialog.dismiss();//关闭方法
36             }
37         }).start();
38     }
39 }
水平进度对话框

 

自定义对话框如下:

技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:layout_margin="16dp">
 7 
 8     <EditText
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:hint="用户名"
12         android:id="@+id/et_1"
13         android:layout_margin="16dp"/>
14     <EditText
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:hint="密码"
18         android:inputType="numberPassword"
19         android:id="@+id/et_2"
20         android:layout_margin="16dp"/>
21 </LinearLayout>
22 
23 *登录对话框*
登录对话框
技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 
 7     <ImageView
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:src="@drawable/f8"
11         android:layout_gravity="center"/>
12 </LinearLayout>
13 
14 *自定义对话框*
自定义对话框

 

各种安卓对话框代码合集

标签:

原文地址:http://www.cnblogs.com/TENOKAWA/p/5492754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!