AlertDialog的几种用法

AlertDialog的几种用法xml代码:java代码:

大家好,又见面了,我是你们的朋友全栈君。

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 }

 <span role="heading" aria-level="2">AlertDialog的几种用法<span role="heading" aria-level="2">AlertDialog的几种用法<span role="heading" aria-level="2">AlertDialog的几种用法<span role="heading" aria-level="2">AlertDialog的几种用法<span role="heading" aria-level="2">AlertDialog的几种用法<span role="heading" aria-level="2">AlertDialog的几种用法

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/155132.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号