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

Android 对话框

时间:2016-05-11 16:33:32      阅读:247      评论: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"></activity>
12         <activity android:name=".TestActivity4">
13 
14         </activity>
15         <activity android:name=".Main2Activity"></activity>
16         <activity android:name=".Main3Activity">
17             <intent-filter>
18             <action android:name="android.intent.action.MAIN" />
19 
20             <category android:name="android.intent.category.LAUNCHER" />
21         </intent-filter></activity>
22     </application>
23 
24 </manifest>

2.activity_mani3.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.android.Main3Activity"
    android:orientation="vertical">
    <!--对话框-->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="一般对话框"
            android:onClick="bt1_onclick"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="单选对话框"
            android:onClick="bt2_onclick"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="复选对话框"
        android:onClick="bt3_onclick"/>
</LinearLayout>

3.Mani3Activity.java

【一般对话框的添加】

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

【单选对话框】

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

【复选对话框】

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

 

Android 对话框

标签:

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

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