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>

资源下载链接为: https://pan.quark.cn/s/140386800631 通用大模型文本分类实践的基本原理是,借助大模型自身较强的理解和推理能力,在使用时需在prompt中明确分类任务目标,并详细解释每个类目概念,尤其要突出类目间的差别。 结合in-context learning思想,有效的prompt应包含分类任务介绍及细节、类目概念解释、每个类目对应的例子和待分类文本。但实际应用中,类目和样本较多易导致prompt过长,影响大模型推理效果,因此可先通过向量检索缩小范围,再由大模型做最终决策。 具体方案为:离线时提前配置好每个类目的概念及对应样本;在线时先对给定query进行向量召回,再将召回结果交给大模型决策。 该方法不更新任何模型参数,直接使用开源模型参数。其架构参考GPT-RE并结合相关实践改写,加入上下文学习以提高准确度,还使用BGE作为向量模型,K-BERT提取文本关键词,拼接召回的相似例子作为上下文输入大模型。 代码实现上,大模型用Qwen2-7B-Instruct,Embedding采用bge-base-zh-v1.5,向量库选择milvus。分类主函数的作用是在向量库中召回相似案例,拼接prompt后输入大模型。 结果方面,使用ICL时accuracy达0.94,比bert文本分类的0.98低0.04,错误类别6个,处理时添加“家居”类别,影响不大;不使用ICL时accuracy为0.88,错误58项,可能与未修改prompt有关。 优点是无需训练即可有较好结果,例子优质、类目界限清晰时效果更佳,适合围绕通用大模型api打造工具;缺点是上限不高,仅针对一个分类任务部署大模型不划算,推理速度慢,icl的token使用多,用收费api会有额外开销。 后续可优化的点是利用key-bert提取的关键词,因为核心词语有时比语意更重要。 参考资料包括
内容概要:本文详细介绍了哈希表及其相关概念和技术细节,包括哈希表的引入、哈希函数的设计、冲突处理机制、字符串哈希的基础、哈希错误率分析以及哈希的改进与应用。哈希表作为一种高效的数据结构,通过键值对存储数据,能够快速定位和检索。文中讨论了整数键值和字符串键值的哈希方法,特别是字符串哈希中的多项式哈希及其优化方法,如双哈希和子串哈希的快速计算。此外,还探讨了常见的冲突处理方法——拉链法和闭散列法,并提供了C++实现示例。最后,文章列举了哈希在字符串匹配、最长回文子串、最长公共子字符串等问题中的具体应用。 适合人群:计算机科学专业的学生、算法竞赛选手以及有一定编程基础并对数据结构和算法感兴趣的开发者。 使用场景及目标:①理解哈希表的工作原理及其在各种编程任务中的应用;②掌握哈希函数的设计原则,包括如何选择合适的模数和基数;③学会处理哈希冲突的方法,如拉链法和闭散列法;④了解并能运用字符串哈希解决实际问题,如字符串匹配、回文检测等。 阅读建议:由于哈希涉及较多数学知识和编程技巧,建议读者先熟悉基本的数据结构和算法理论,再结合代码实例进行深入理解。同时,在实践中不断尝试不同的哈希策略,对比性能差异,从而更好地掌握哈希技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值