游戏技术文章

AlertDialog的几种用法

时间:2017-3-12 14:27:28  作者:棋牌资源网  来源:棋牌资源网  查看:7857  评论:0
内容摘要:xml代码: 1 2 xml代码:
复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:app="http://schemas.android.com/apk/res-auto"
 5     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="com.example.lesson7_3_id19_alertdialog.MainActivity">
 9 
10     <Button
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:text="简单的dialog"
14         android:onClick="dialog_1"/>
15     <Button
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="列表的dialog"
19         android:onClick="dialog_2"/>
20     <Button
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="单选的dialog"
24         android:onClick="dialog_3"/>
25     <Button
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:text="多选的dialog"
29         android:onClick="dialog_4"/>
30     <Button
31         android:layout_width="match_parent"
32         android:layout_height="wrap_content"
33         android:text="自定义View的dialog"
34         android:onClick="dialog_5"/>
35     <Button
36         android:layout_width="match_parent"
37         android:layout_height="wrap_content"
38         android:text="使用adapter的dialog"
39         android:onClick="dialog_6"/>
40 </LinearLayout>
复制代码

java代码:

复制代码
  1 package com.example.lesson7_3_id19_alertdialog;
  2 
  3 import android.app.AlertDialog;
  4 import android.content.DialogInterface;
  5 import android.os.Bundle;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.view.View;
  8 import android.widget.ArrayAdapter;
  9 import android.widget.EditText;
 10 import android.widget.Toast;
 11 
 12 public class MainActivity extends AppCompatActivity {
 13 
 14     @Override
 15     protected void onCreate(Bundle savedInstanceState) {
 16         super.onCreate(savedInstanceState);
 17         setContentView(R.layout.activity_main);
 18     }
 19     public void dialog_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("确定",new DialogInterface.OnClickListener() {
 26             @Override
 27             public void onClick(DialogInterface dialog, int which) {
 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 void dialog_2(View v){
 39         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 40         builder.setTitle("请选择");
 41         builder.setItems(item, new DialogInterface.OnClickListener() {
 42             @Override
 43             public void onClick(DialogInterface dialog, int which) {
 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     int index;
 54     public void dialog_3(View v){
 55         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 56         builder.setTitle("请选择");
 57         builder.setSingleChoiceItems(item, index, new DialogInterface.OnClickListener() {
 58             @Override
 59             public void onClick(DialogInterface dialog, int which) {
 60                 index = which;
 61             }
 62         });
 63         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
 64             @Override
 65             public void onClick(DialogInterface dialog, int which) {
 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 void dialog_4(View v){
 76         AlertDialog.Builder builder = new AlertDialog.Builder(this);
 77         builder.setTitle("请选择");
 78         builder.setMultiChoiceItems(item, bools, new DialogInterface.OnMultiChoiceClickListener() {
 79             @Override
 80             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
 81                 bools[which] = isChecked;
 82             }
 83         });
 84         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
 85             @Override
 86             public void onClick(DialogInterface dialog, int which) {
 87                 StringBuffer sb = new StringBuffer();
 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 void dialog_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("确定", new DialogInterface.OnClickListener() {
109             @Override
110             public void onClick(DialogInterface dialog, int which) {
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 void dialog_6(View v){
124         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,item);
125         AlertDialog.Builder builder = new AlertDialog.Builder(this);
126         builder.setTitle("使用适配器");
127         builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
128             @Override
129             public void onClick(DialogInterface dialog, int which) {
130                 Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();
131             }
132         });
133         AlertDialog alertDialog = builder.create();
134         alertDialog.show();
135     }
136 }

标签:AlertDialog的几种用法 

欢迎加入VIP,【VIP售价:只要288元永久VIP会员】畅享商业棋牌游戏程序下载,点击开通!

下载说明


☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,

☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!

☉唯一站长QQ:1004003180  [人格担保-本站注重诚信!]

☉购买建议E-mail:1004003180@qq.com   源码收购 E-mail:1004003180@qq.com    

☉本站文件解压密码  【文章内都自带解压密码,每个密码不同!】


本站提供的所有源码,均来源站长提供,仅学习交流 浙ICP备09009969号

由此产生不良后果和法律责任与本站无关,如果侵犯了您的版权,请来信告知 1004003180@qq.com 将及时更正和删除! 

Copyright © 2008-2024 棋牌资源网,你身边的棋牌资源下载站    All Rights Reserved