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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
运行结果:
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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
运行结果:
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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
运行结果:
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();
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
运行结果:
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();
? ? }
}
运行结果:
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();
? ? }
}
运行结果:
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();
? ? }
}
运行结果:
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();
? ? }
}
运行结果:
来源:https://blog.csdn.net/weixin_43468667/article/details/84193930