Android对话框使用方法详解

作者:明昕1024 时间:2023-11-09 03:37:51 

对话框(Dialog)是Android系统在Activity或者其他组件运行过程中提供的一种提示机制。它可以帮助应用完成一些必要的提示功能,同时提供一些与用户交互的功能。

对话框分为很多种,下面将一一介绍。

1.提示对话框

Android系统提供的对话框父类为Dialog, 里面并没有实现对话框的具体类型,比如单选、多选、列表等对话框,仅提供一个框架和规范。系统为开发者提供了一个 多功能的对话框类AlertDialog, 里面封装了各种对话框的样式,开发者只须提供相应的显示数据和按键的响应监听就可以。

提示对话框所使用的就是系统封装好的对话框AlertDialog的实例对象。AlertDialog并不提供对外的构造方法,即不能直接通过AlertDialog的构造函数来生产一个AlertDialog。因为AlertDialog所有的构造方法都是protected的。所以为了获取AlertDialog对象,系统提供了一个静态内部类Builder让我们使用,通过Builder可以创建AlertDialog对象。

(1)创建AlertDialog.Builder实例对象。

(2)通过Builder对象设置对话框的属性。

  • setTitle()设置标题

  • setIcon ()设置图标

  • setMessage ()设置要显示的内容

  • setItems设置在对话框中显示的项目列表

  • setView设置自定义的对话框样式

  • setPositiveButton ()设置确定按钮

  • setNegativeButton ()设置取消按钮

  • setNeutralButton ()设置中立按钮

  • setSingleChoiceItems单选框

  • setMultiChoiceItems复选框

(3)调用Builder对象的create()方法创建AlertDialog对话框

(4)调用AlertDialog的show()方法来显示对话框

(5)调用AlertDialog的dimiss()方法销毁对话框。

package com.example.learndialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button bt= (Button) findViewById(R.id.button);
? ? ? ? bt.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? AlertDialog aldg;
? ? ? ? ? ? ? ? AlertDialog.Builder adBd=new AlertDialog.Builder(MainActivity.this);
? ? ? ? ? ? ? ? adBd.setTitle("我的提示框");
? ? ? ? ? ? ? ? adBd.setIcon(R.drawable.p1);
? ? ? ? ? ? ? ? adBd.setMessage("确定要关闭本应用程序吗?");
? ? ? ? ? ? ? ? adBd.setPositiveButton("确定", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? finish();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? adBd.setNegativeButton("取消", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {

? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? aldg=adBd.create();
? ? ? ? ? ? ? ? aldg.show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

运行结果:

Android对话框使用方法详解

2.单选对话框

单选对话框中的0代表默认选中第一个。

package com.example.learndialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
? ? int picWhich;

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button bt = (Button) findViewById(R.id.button);
? ? ? ? bt.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? final String[] starr = new String[]{"教师", "经理", "律师", "公务员"};
? ? ? ? ? ? ? ? AlertDialog adlg;
? ? ? ? ? ? ? ? adlg = new AlertDialog.Builder(MainActivity.this).setTitle("选择职业:")
? ? ? ? ? ? ? ? ? ? ? ? .setSingleChoiceItems(starr, 0, new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? picWhich = which;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("确定", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "你选定的职业是:"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + starr[picWhich], Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .create();
? ? ? ? ? ? ? ? adlg.show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

运行结果:

Android对话框使用方法详解

3.复选对话框

复选对话框和单选对话框用法相似。

package com.example.learndialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
? ? int picWhich;
? ? boolean chk[]=new boolean[]{false,false,false,false};
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button bt = (Button) findViewById(R.id.button);
? ? ? ? bt.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? final String[] starr = new String[]{"教师", "经理", "律师", "公务员"};
? ? ? ? ? ? ? ? AlertDialog adlg;
? ? ? ? ? ? ? ? adlg = new AlertDialog.Builder(MainActivity.this).setTitle("选择职业:")
? ? ? ? ? ? ? ? ? ? ? ? .setMultiChoiceItems(starr, chk, new DialogInterface.OnMultiChoiceClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which, boolean isChecked) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? chk[which]=isChecked;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .setPositiveButton("确定", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String st=" ";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i=0;i<chk.length;i++)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (chk[i])
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? st=st+starr[i]+" ";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this,"选定的职业有:"+st,Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .create();
? ? ? ? ? ? ? ? adlg.show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

运行结果:

Android对话框使用方法详解

4.列表对话框

列表对话框和单选对话框用法相似。

package com.example.learndialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Button bt = (Button) findViewById(R.id.button);
? ? ? ? bt.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? final String[] starr = new String[]{"教师", "经理", "律师", "公务员"};
? ? ? ? ? ? ? ? AlertDialog adlg;
? ? ? ? ? ? ? ? adlg=new AlertDialog.Builder(MainActivity.this).setTitle("列表对话框")
? ? ? ? ? ? ? ? ? ? ? ? .setItems(starr, new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? ? ? ? ? .create();
? ? ? ? ? ? ? ? adlg.show();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

运行结果:

Android对话框使用方法详解

5.进度条对话框

ProgressDialog 也是继承于Dialog,但其扩展了缓冲加载提示的功能,为人机之间提供了良好的交互体验。

package com.example.learndialog;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }
? ? public void progress_circle(View v){
? ? ? ? final ProgressDialog prdg1=new ProgressDialog(MainActivity.this);
? ? ? ? prdg1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
? ? ? ? prdg1.setTitle("圆形进度条对话框");
? ? ? ? prdg1.setMessage("正在下载");
? ? ? ? prdg1.setMax(100);
? ? ? ? prdg1.show();
? ? ? ? new Thread(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? Thread.sleep(5000);
? ? ? ? ? ? ? ? ? ? prdg1.cancel();
? ? ? ? ? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }).start();
? ? }
? ? public void progress_horizontal(View v){
? ? ? ? final ProgressDialog prdg2 = new ProgressDialog(this);
? ? ? ? prdg2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
? ? ? ? prdg2.setCancelable(true);
? ? ? ? prdg2.setTitle("水平进度条对话框");
? ? ? ? prdg2.setMax(100);
? ? ? ? prdg2.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? prdg2.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
? ? ? ? ? ? ? ? new DialogInterface.OnClickListener() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? ? ? prdg2.setMessage("正在下载");
? ? ? ? prdg2.show();
? ? ? ? new Thread(new Runnable() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? int i = 0;
? ? ? ? ? ? ? ? while (i < 100) {
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? Thread.sleep(100);
? ? ? ? ? ? ? ? ? ? ? ? prdg2.incrementProgressBy(1);
? ? ? ? ? ? ? ? ? ? ? ? i++;
? ? ? ? ? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? prdg2.dismiss();
? ? ? ? ? ? }
? ? ? ? }).start();
? ? }

}

运行结果:

Android对话框使用方法详解

6.拖动对话框

package com.example.learndialog;

import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? }
? ? public void SeekOnClick(View v){
? ? ? ? Dialog myDlg=new Dialog(MainActivity.this);
? ? ? ? myDlg.setTitle("拖动对话框:亮度调节");
? ? ? ? myDlg.setContentView(R.layout.seekbardlg);
? ? ? ? SeekBar sb=(SeekBar)myDlg.findViewById(R.id.seekBar);
? ? ? ? final TextView tv=(TextView)myDlg.findViewById(R.id.textView);
? ? ? ? sb.setMax(100);
? ? ? ? sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
? ? ? ? ? ? ? ? tv.setText("当前亮度为:"+progress);
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onStartTrackingTouch(SeekBar seekBar) {

? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onStopTrackingTouch(SeekBar seekBar) {

? ? ? ? ? ? }
? ? ? ? });
? ? ? ? myDlg.show();
? ? }
}

运行结果:

Android对话框使用方法详解

7.日期选择对话框

package com.example.datepickerdialog;

import android.app.DatePickerDialog;
import android.icu.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? Calendar c=Calendar.getInstance();
? ? ? ? DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onDateSet(DatePicker view, int year, int month, int day) {
? ? ? ? ? ? ? ? String st;
? ? ? ? ? ? ? ? st = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day);
? ? ? ? ? ? ? ? Toast.makeText(getApplicationContext(), st, Toast.LENGTH_LONG).show();
? ? ? ? ? ? }

? ? ? ? },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
? ? ? ? dpd.show();
? ? }
}

运行结果:

Android对话框使用方法详解

8.时间选择对话框

package com.example.datepickerdialog;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.icu.util.Calendar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ?Calendar calendar=Calendar.getInstance();
? ? ? ? new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

? ? ? ? ? ? }
? ? ? ? },
? ? ? ? ? ? ? ? calendar.get(Calendar.HOUR_OF_DAY),
? ? ? ? ? ? ? ? calendar.get(Calendar.MINUTE),
? ? ? ? ? ? ? ? true
? ? ? ? ).show();

? ? }
}

运行结果:

Android对话框使用方法详解

来源:https://blog.csdn.net/weixin_43468667/article/details/84193930

标签:Android,对话框
0
投稿

猜你喜欢

  • Spring Boot实现分布式锁的自动释放的示例代码

    2023-10-17 11:06:24
  • Android实现与Apache Tomcat服务器数据交互(MySql数据库)

    2023-06-02 21:53:39
  • 解析StreamReader与文件乱码问题的解决方法

    2022-09-11 18:34:56
  • Java带复选框的树(Java CheckBox Tree)实现和应用

    2021-07-25 20:53:46
  • 详解关于java文件下载文件名乱码问题解决方案

    2021-07-05 15:27:04
  • Android Handle原理(Looper,Handler和Message)三者关系案例详解

    2023-08-25 22:51:47
  • Java异常学习之自定义异常详解

    2023-09-25 00:57:27
  • Java实现将图片上传到webapp路径下 路径获取方式

    2023-07-10 12:44:13
  • spring boot学习笔记之操作ActiveMQ指南

    2023-09-12 20:11:52
  • 浅谈抛出异常和捕获异常的一些区别

    2023-10-19 15:25:24
  • C#中派生类调用基类构造函数用法分析

    2022-01-14 08:10:22
  • 关于springboot中对sqlSessionFactoryBean的自定义

    2022-12-09 06:05:09
  • 逆波兰计算器(Java实现)

    2021-10-18 09:45:01
  • C语言二叉树的非递归遍历实例分析

    2022-02-08 02:41:15
  • 关于Java 中 Future 的 get 方法超时问题

    2022-09-27 07:58:18
  • C语言之如何求三次方根

    2022-04-30 03:13:52
  • springboot 按月分表的实现方式

    2023-11-25 00:03:47
  • 使用IDEA配置Maven搭建开发框架ssm教程

    2023-04-05 13:33:21
  • Java实现TFIDF算法代码分享

    2023-12-23 20:54:45
  • Android 监听屏幕是否锁屏的实例代码

    2022-01-19 14:31:24
  • asp之家 软件编程 m.aspxhome.com