Android Dialog汇总

本文详细介绍Android应用中多种对话框的实现方法,包括普通对话框、多按钮对话框、列表对话框、选择对话框(单选和多选)、进度条对话框及读取对话框等。通过具体的代码实例展示了如何创建和使用这些对话框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文章为转载文章,博客原地址如下:

http://liwenquan.top/2016/04/03/Android-Dialog/

在Android开发中,经常会需要在Android界面上弹出一些对话框。
分类:

  1. 普通的对话框
  2. 多按钮对话框
  3. 列表对话框
  4. 选择对话框(单选和多选)
  5. 进度条对话框
  6. 读取对话框

上代码

MainActivity.class

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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.liwenquan.dailogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends Activity implements Runnable {
    public final static int MAX_READPROCESS = 100;
    /*单项选择对话框*/
    int yourChose = -1;
    ArrayList<Integer> myChose = new ArrayList<Integer>();
    //进度读取框需要模拟读取
    ProgressDialog mReadProcessDia = null;
    private Button btn_diaNormal;
    private Button btn_diaMulti;
    private Button btn_diaList;
    private Button btn_diaSinChos;
    private Button btn_diaMultiChos;
    private Button btn_diaProcess;
    private Button btn_diaReadProcess;
    private PopupWindow window = null;
    private Button.OnClickListener btnListener = new Button.OnClickListener() {
        public void onClick(View v) {
            if (v instanceof Button) {
                int btnId = v.getId();
                switch (btnId) {
                    case R.id.btn_diaNormal:
                        showNormalDia();
                        break;
                    case R.id.btn_diaMulti:
                        showMultiDia();
                        break;
                    case R.id.btn_diaList:
                        showListDia();
                        break;
                    case R.id.btn_diaSigChos:
                        showSinChosDia();
                        break;
                    case R.id.btn_diaMultiChos:
                        showMultiChosDia();
                        break;
                    case R.id.btn_diaReadProcess:
                        showReadProcess();
                        break;
                    case R.id.btn_diaProcess:
                        showProcessDia();
                        break;
                    default:
                        break;
                }
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getView();
        setListener();
    }

    private void getView() {
        btn_diaNormal = (Button) findViewById(R.id.btn_diaNormal);
        btn_diaMulti = (Button) findViewById(R.id.btn_diaMulti);
        btn_diaList = (Button) findViewById(R.id.btn_diaList);
        btn_diaSinChos = (Button) findViewById(R.id.btn_diaSigChos);
        btn_diaMultiChos = (Button) findViewById(R.id.btn_diaMultiChos);
        btn_diaProcess = (Button) findViewById(R.id.btn_diaProcess);
        btn_diaReadProcess = (Button) findViewById(R.id.btn_diaReadProcess);

    }

    private void setListener() {
        btn_diaNormal.setOnClickListener(btnListener);
        btn_diaMulti.setOnClickListener(btnListener);
        btn_diaList.setOnClickListener(btnListener);
        btn_diaSinChos.setOnClickListener(btnListener);
        btn_diaMultiChos.setOnClickListener(btnListener);
        btn_diaProcess.setOnClickListener(btnListener);
        btn_diaReadProcess.setOnClickListener(btnListener);
    }

    /*普通的对话框*/
    private void showNormalDia() {
        //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());
        AlertDialog.Builder normalDia = new AlertDialog.Builder(MainActivity.this);
        //normalDia.setIcon(R.drawable.ic_launcher);
        normalDia.setTitle("普通的对话框");
        normalDia.setMessage("普通对话框的message内容");

        normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                showClickMessage("确定");
            }
        });
        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                showClickMessage("取消");
            }
        });
        normalDia.create().show();
    }

    /*多按钮对话框*/
    private void showMultiDia() {
        AlertDialog.Builder multiDia = new AlertDialog.Builder(MainActivity.this);
        multiDia.setTitle("多选项对话框");
        multiDia.setPositiveButton("按钮一", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                showClickMessage("按钮一");
            }
        });
        multiDia.setNeutralButton("按钮二", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                showClickMessage("按钮二");
            }
        });
        multiDia.setNegativeButton("按钮三", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                showClickMessage("按钮三");
            }
        });
        multiDia.create().show();
    }

    /*列表对话框*/
    private void showListDia() {
        final String[] mList = {"选项1", "选项2", "选项3"};
        AlertDialog.Builder listDia = new AlertDialog.Builder(MainActivity.this);
        listDia.setTitle("列表对话框");
        listDia.setItems(mList, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                /*下标是从0开始的*/
                showClickMessage(mList[which]);
            }
        });
        listDia.create().show();
    }

    private void showSinChosDia() {
        final String[] mList = {"选项1", "选项2", "选项3"};
        yourChose = -1;
        AlertDialog.Builder sinChosDia = new AlertDialog.Builder(MainActivity.this);
        sinChosDia.setTitle("单项选择对话框");
        sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                yourChose = which;

            }
        });
        sinChosDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                if (yourChose != -1) {
                    showClickMessage(mList[yourChose]);
                }
            }
        });
        sinChosDia.create().show();
    }

    private void showMultiChosDia() {
        final String[] mList = {"选项1", "选项2", "选项3"};
        final boolean mChoseSts[] = {false, false, false};
        myChose.clear();
        AlertDialog.Builder multiChosDia = new AlertDialog.Builder(MainActivity.this);
        multiChosDia.setTitle("多项选择对话框");
        multiChosDia.setMultiChoiceItems(mList, mChoseSts, new DialogInterface.OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    myChose.add(which);
                } else {
                    myChose.remove(which);
                }
            }
        });
        multiChosDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                int size = myChose.size();
                String str = "";
                for (int i = 0; i < size; i++) {
                    str += mList[myChose.get(i)];
                }
                showClickMessage(str);
            }
        });
        multiChosDia.create().show();
    }

    private void showReadProcess() {
        mReadProcessDia = new ProgressDialog(MainActivity.this);
        mReadProcessDia.setProgress(0);
        mReadProcessDia.setTitle("进度条窗口");
        mReadProcessDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mReadProcessDia.setMax(MAX_READPROCESS);
        mReadProcessDia.show();
        new Thread(this).start();
    }

    //新开启一个线程,循环的累加,一直到100然后在停止
    @Override
    public void run() {
        int Progress = 0;
        while (Progress < MAX_READPROCESS) {
            try {
                Thread.sleep(100);
                Progress++;
                mReadProcessDia.incrementProgressBy(1);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //读取完了以后窗口自消失
        mReadProcessDia.cancel();
    }

    /*读取中的对话框*/
    private void showProcessDia() {
        ProgressDialog processDia = new ProgressDialog(MainActivity.this);
        processDia.setTitle("进度条框");
        processDia.setMessage("内容读取中...");
        processDia.setIndeterminate(true);
        processDia.setCancelable(true);
        processDia.show();
    }


    /*显示点击的内容*/
    private void showClickMessage(String message) {
        Toast.makeText(MainActivity.this, "你选择的是: " + message, Toast.LENGTH_SHORT).show();
    }
}

activity_main.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="各种Dialog合集" />

    <Button
        android:id="@+id/btn_diaNormal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="普通Dialog" />

    <Button
        android:id="@+id/btn_diaMulti"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="多按钮Dialog" />

    <Button
        android:id="@+id/btn_diaList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="列表Dialog" />

    <Button
        android:id="@+id/btn_diaSigChos"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="单项选择Dialog" />

    <Button
        android:id="@+id/btn_diaMultiChos"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="多项选择Dialog" />

    <Button
        android:id="@+id/btn_diaReadProcess"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="进度条Dialog" />

    <Button
        android:id="@+id/btn_diaProcess"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="读取中Dialog" />

</LinearLayout>

内容概要:本文详细探讨了基于MATLAB/SIMULINK的多载波无线通信系统仿真及性能分析,重点研究了以OFDM为代表的多载波技术。文章首先介绍了OFDM的基本原理和系统组成,随后通过仿真平台分析了不同调制方式的抗干扰性能、信道估计算法对系统性能的影响以及同步技术的实现与分析。文中提供了详细的MATLAB代码实现,涵盖OFDM系统的基本仿真、信道估计算法比较、同步算法实现和不同调制方式的性能比较。此外,还讨论了信道特征、OFDM关键技术、信道估计、同步技术和系统级仿真架构,并提出了未来的改进方向,如深度学习增强、混合波形设计和硬件加速方案。; 适合人群:具备无线通信基础知识,尤其是对OFDM技术有一定了解的研究人员和技术人员;从事无线通信系统设计与开发的工程师;高校通信工程专业的高年级本科生和研究生。; 使用场景及目标:①理解OFDM系统的工作原理及其在多径信道环境下的性能表现;②掌握MATLAB/SIMULINK在无线通信系统仿真中的应用;③评估不同调制方式、信道估计算法和同步算法的优劣;④为实际OFDM系统的设计和优化提供理论依据和技术支持。; 其他说明:本文不仅提供了详细的理论分析,还附带了大量的MATLAB代码示例,便于读者动手实践。建议读者在学习过程中结合代码进行调试和实验,以加深对OFDM技术的理解。此外,文中还涉及了一些最新的研究方向和技术趋势,如AI增强和毫米波通信,为读者提供了更广阔的视野。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值