Android对话框总结

article/2025/10/29 6:04:41
一、什么是对话框?
一种次要窗口,包含按钮和各种选项,通过它们可以完成特定命令或任务。 查找和替换对话框 对话框与窗口有区别,它没有最大化按钮、没有最小化按钮、大都不能改变形状大小。(“打开文件”对话框是可以改变大小的) 对话框:是人机交流的一种方式,用户对对话框进行设置,计算机就会执行相应的命令。对话框中有单选框、复选框等。
要我说,对话框,就是一个用于和用户进行对话的小框框。大家见到最多的那种是弹出对话框。
二、对话框有什么用?
对话框不会阻塞主线程,可以和用户进行简单交互,且给人一种清新的感觉,好的对话框不仅不会影响用户体验,反而会让用户喜欢上这种被打扰的感觉。

三、怎么用?

         

1、系统对话框
AlertDialog
ProgressDialog
DatePickerDialog
TimePickerDialog
2、自定义对话框
a、使用LayoutInflate和Builder的setView方法实现
b、继承Dialog类复写setContentView实现
实例:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/show1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/button_background"
        android:text="dialog1" />

    <Button
        android:id="@+id/show2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/button_background"
        android:text="dialog2" />

    <Button
        android:id="@+id/show3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/button_background"
        android:text="dialog3" />

</LinearLayout>

activity_dialog.xml
<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:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="标题"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="这是一个自定义布局的AlertDialog" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="有木有很好玩" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="想怎么布局就怎么布局"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_background"
        android:text="确定" />

</LinearLayout>

MainActivity.java
public class MainActivity extends Activity {
    private Button show1, show2, show3;
    DatePickerDialog m = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show1 = (Button) findViewById(R.id.show1);
        show2 = (Button) findViewById(R.id.show2);
        show3 = (Button) findViewById(R.id.show3);
        show1.setOnClickListener(mClickListener);
        show2.setOnClickListener(mClickListener);
        show3.setOnClickListener(mClickListener);
    }

    private View.OnClickListener mClickListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            int id = arg0.getId();
            switch (id) {
            case R.id.show1:
                showDialog1();
                break;
            case R.id.show2:
                showDialog2();
                break;
            case R.id.show3:
                showDialog3();
                break;
            default:
                break;
            }
        }
    };

     private void showDialog1() {
        new AlertDialog.Builder(this).setTitle("dialog 1")
                .setMessage("这是最简单的AlertDialog").setPositiveButton("确定", null)
                .setNeutralButton("什么", null).setNegativeButton("取消", null)
                .create().show();
    }

    

     private void showDialog2(){
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.activity_dialog, null);
        final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();
        Button ok = (Button) view.findViewById(R.id.button1);
        ok.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    
    private void showDialog3(){
        new MyDialog(this).show();
    }
    
    private class MyDialog extends Dialog{

        public MyDialog(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            this.setContentView(R.layout.activity_dialog);
            this.setTitle("標題");
            Button b = (Button) findViewById(R.id.button1);
            b.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    dismiss();
                }
            });
        }

    }
}

四、注意总结
注意:
1、不要使对话框泛滥
2、自定义对话框时要注意dismiss()或则不用时remove()
3、还有一种定义对话框的方法,那就是把Activity的主题设置为Theme.Dialog
总结:
在这个 看颜的时代,要想成为一个好的程序员,首先要成为一名好的设计师。


http://chatgpt.dhexx.cn/article/210ygNjQ.shtml

相关文章

Android常用对话框

学习目标&#xff1a; 两周入门Android 学习内容&#xff1a; 常见对话框: 对话框是程序与用户交互的一种方式&#xff0c;通常用于显示当前程序提示信息以及相关说明,以小窗口形式展现 普通对话框&#xff1a; 通过AlertDialog.Builder(this).create来创建对话框最后通过…

了解Android布局,了解Android对话框布局

我正在为我的android应用程序做一个简单的自定义对话框&#xff0c;只显示一个搜索栏。然而&#xff0c;这个简单任务的复杂性让我很烦。了解Android对话框布局 我的对话框布局如下&#xff1a;在代码中创建 android:orientation"vertical" android:layout_width&quo…

Android对话框(普通对话框、单选对话框、多选对话框、进度条对话框)

一、普通对话框 // 通过builder 构建器来构造AlertDialog.Builder builder = new Builder(this);builder.setTitle("警告");builder.setMessage("你好么 ");builder.setPositiveButton("好", new OnClickListener() {@Overridepublic void onCl…

android 对话框窗口,Android 对话框详解(一)

对话框是程序运行中的弹出窗口。例如&#xff0c;当用户要删除一个联系方式时&#xff0c;会弹出一个 对话框&#xff0c;让用户确认是否真的要删除。 Android系统提供了四种对话框:警告对话框 (AlertDialog)、进度对话框(ProgressDialog)、日期选择对话框(DatePickerDialog)和…

Android对话框

(不足之处,多多担待) 一: 对话框的种类 1:提示对话框 2:自定义对话框 3:弹出对话框 二:设置对话框的详细步骤 1:提示对话框 在activity中设置代码实现功能 , 第一步实例化对话框 第二步是设置对话框的提示内容 ,有4个主要方法 (窗贴提示内容:setTitle() / setMessage()…

Android常用对话框大全——Dialog

唉&#xff01;最近一直忙碌着写项目以至于都没有空出点时间来总结近期的学习&#xff0c; 记录学习到的东西…现在正好有时间了就该好好记录一下学习的过程了。 今天就来谈谈开发中经常用的到的一个控件——Dialog&#xff0c;对话框一般我们就用来提示一些信息给用户&#…

Android 对话框(Dialog)

对话框是提示用户做出决定或输入额外事件的小窗口。对话框不会填充屏幕&#xff0c;通常用于需要用户采取行动才能继续执行的模式事件。 Dialog类是对话框的基类&#xff0c;我们可以使用Dialog来构建一个对话框。但Android建议避免直接使用Dialog&#xff0c;而应该使用其子类…

android十大常用对话框

Android十大常用对话框 一、对话框效果二、代码 最近老师叫我们整理android常用的对话框&#xff0c;我整理了十种对话框&#xff0c;用于分享交流&#xff0c;希望指正&#xff01; 一、对话框效果 主界面 1.普通对话框 2.单选对话框 3.多选对话框 4.列表对话框 5.不带进…

Android常用的几种对话框

1文本提示对话框 AlertDialog.Builder b new AlertDialog.Builder(this);//this为上下文&#xff0c;如果在本类里显示&#xff0c;通常使用this b.setTitle("标题");/对话框标题 b.setMessage("可能会删除某个文件");//提示文本 …

Android对话框的详细介绍(提示对话框,自定义对话框)

简介&#xff1a; 我们都知道在Android开发中&#xff0c;当我们的程序在与用户交互时&#xff0c;用户会得到一定的反馈&#xff0c;其中以对话框的形式的反馈还是比较常见的&#xff0c;接下来我们来介绍几种常见的对话框的基本使用。 前置准备&#xff1a;&#xff08;文章…

WinInet 和 WinHttp 有何区别?

背景 WinInet和WinHttp是windows平台下提供了两套独立的网络库&#xff0c;按照微软官方的说法&#xff0c; WinInet的优势在于client-端的应用&#xff0c;而WinHttp更适用于server-端编程。从名称上我们可以看出WinHttp在Http协议应用方面要比WinInet更加专业&#xff0c;Win…

使用WinHTTP与服务器通讯

WinHTTP 的工作流程如下 一.初始化WinHTTP 在与服务器交互之前, 必须用调用WinHttpOpen进行初始化,WinHttpOpen创建一个会话,并返回该会话的句柄,接着有了这个句柄, WinHttpConnect就能指定一个目标服务器 注意:调用了WinHttpConnect并不意味着和服务器建立了真正的连接 二.打…

使用c++ winhttp实现post请求

winhttp是windows网络库&#xff0c;要测试自己写的post请求是否有效&#xff0c;首先得在postman上面建立一个可用的接口。我的如下。 代码思路如下&#xff1a; 1、首先使用WinHttpCrackUrl拆解链接&#xff0c;后面会使用到拆解出来的信息。 2、再使用WinHttpOpen初始化 3、…

HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)

玩过抓包&#xff0c;网络协议分析的朋友肯定都知道http https post get&#xff0c;web端和用户的交互主要是通过post get完成的。 今天带给大家的是C版本的http https get post,只会易语言的朋友请移步。 我这里有两种实现&#xff1a; 1&#xff1a;libcurl实现的CHttpClien…

C++用winhttp实现https访问服务器

由于项目升级&#xff0c;在数据传输过程中需要经过OAuth2.0认证&#xff0c;访问服务器需要https协议。 首先&#xff0c;实现C代码访问https 服务器&#xff0c;实现Get和post功能&#xff0c;在网上搜索一通&#xff0c;发现各种各样的都有&#xff0c;有的很简单&#xff0…

实现HTTP协议Get、Post和文件上传功能——使用WinHttp接口实现

在《使用WinHttp接口实现HTTP协议Get、Post和文件上传功能》一文中&#xff0c;我已经比较详细地讲解了如何使用WinHttp接口实现各种协议。在最近的代码梳理中&#xff0c;我觉得Post和文件上传模块可以得到简化&#xff0c;于是几乎重写了这两个功能的代码。因为Get、Post和文…

使用WinINet和WinHTTP实现Http访问

Http访问有两种方式&#xff0c;GET和POST&#xff0c;就编程来说GET方式相对简单点&#xff0c;它不用向服务器提交数据&#xff0c;在这个例程中我使用POST方式&#xff0c;提交数据value1与value2&#xff0c;并从服务器得到他们的和&#xff08;value1 value2&#xff09;…

WinHttp.WinHttpRequest.5.1

Set oHttp CreateObject ( " WinHttp.WinHttpRequest.5.1 " )oHttp.Option( 6 ) 0 禁止自动Redirect&#xff0c;最关键的 oHttp.SetTimeouts 9999999 , 9999999 , 9999999 , 9999999 设置超时&#xff5e;和ServerXMLHTTP组件一样 oHttp.Open " GET &q…

Tinyhttpd for Windows

TinyHTTPd forWindows 前言 TinyHTTPd是一个开源的简易学习型的HTTP服务器&#xff0c;项目主页在&#xff1a;http://tinyhttpd.sourceforge.net/&#xff0c;源代码下载&#xff1a;https://sourceforge.net/projects/tinyhttpd/&#xff0c;因为是学习型的代码,已经有好多年…

windows安装http测试服务

祝您身体健康&#xff0c;前程似锦&#xff0c;小弟期待文章对您有帮助&#xff0c;也期待您的打赏: 目录 1. 下载 HttpMockServer工具 2. windows10 安装jdk1.8 3. jdk按照上面配置完环境变量后打开cmd&#xff0c;运行 4. 准备测试的响应数据 text.txt,内容如下: 5. 会弹…