服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android中AlertDialog四种对话框的最科学编写用法(实例代码)

Android中AlertDialog四种对话框的最科学编写用法(实例代码)

2022-11-09 15:02Geeksongs Android

这篇文章主要介绍了Android中AlertDialog四种对话框的最科学编写用法,本文通过代码讲解的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

首先我们上图:

Android中AlertDialog四种对话框的最科学编写用法(实例代码)

 xml的代码如下,用于编写按钮:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
xmlns:widget="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
  <Button
    android:id="@+id/button_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="简单的dialog"
    />
  <Button
    android:id="@+id/button_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="列表的dialog"
    />
  <Button
    android:id="@+id/button_3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="单选的dialog"
    />
  <Button
    android:id="@+id/button_4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="多选的dialog"
    />
</LinearLayout>

Java代码如下,用于实现逻辑:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
  int index;
  String [] item = {"Android","IOS","Spark","Hadoop","Web"};
  boolean[] bools = {false,false,false,false,false};
  // 设置boolean数组所有的选项设置默认没选
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
      actionBar.hide();
    }
    Button button=(Button)findViewById(R.id.button_1);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setIcon(R.drawable.girl);
        builder.setTitle("标题栏");
        builder.setMessage("对话框内容,可自行设置");
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNeutralButton("好的", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "点击了“好的”", Toast.LENGTH_SHORT).show();
          }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
    Button button2=(Button)findViewById(R.id.button_2);
    button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请选择一个技术分支");
        builder.setItems(item, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "选择了"+item[which], Toast.LENGTH_SHORT).show();
          }
        });
        // 取消可以不添加
        //builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
    Button button3=(Button)findViewById(R.id.button_3);
    button3.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请选择技术分支:");
        builder.setSingleChoiceItems(item, index, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            index = which;
          }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(MainActivity.this, "选择了"+item[index], Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
    Button button4=(Button)findViewById(R.id.button_4);
    button4.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("请选择技术分支:");
        builder.setMultiChoiceItems(item, bools, new DialogInterface.OnMultiChoiceClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            bools[which] = isChecked;
          }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < item.length; i++) {
              if (bools[i]) {
                sb.append(item[i] + " ");
              }
            }
            Toast.makeText(MainActivity.this, "选择了" + sb.toString(), Toast.LENGTH_SHORT).show();
          }
        });
        builder.setNegativeButton("取消",null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
      }
    });
  }
}

总结

以上所述是小编给大家介绍的Android中AlertDialog四种对话框的最科学编写用法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/geeksongs/archive/2019/11/24/11922012.html

延伸 · 阅读

精彩推荐