Android 七种对话框使用
2017-03-04 19:34
85 查看
Android 七种对话框使用
简单对话框简单列表对话框
单选列表对话框
多选列表对话框
进度条对话框
编辑对话框
自定义对话框
简单对话框
private void showNormalDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("简单对话框") .setIcon(R.mipmap.ic_launcher) .setMessage("这是一个简单对话框") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //定义自己想要做的操作 showText("确定"); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText("取消"); } }); builder.create().show(); }
简单列表对话框
private void showListDialog() { final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("简单列表对话框"); //千万不要加这句,不然列表显示不出来 // builder.setMessage("这是一个简单的列表对话框"); builder.setIcon(R.mipmap.ic_launcher); builder.setItems(items, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText(items[which]); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText("确定"); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText("取消"); } }); builder.create().show(); }
单选列表对话框
private void showSingleChoiceDialog() { final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("简单列表对话框"); //千万不要加这句,不然列表显示不出来 // builder.setMessage("这是一个简单的列表对话框"); builder.setIcon(R.mipmap.ic_launcher); builder.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText(items[which]); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText("确定"); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText("取消"); } }); builder.create().show(); }
多选列表对话框
private void showMultiChoiceDialog() { final String[] items = new String[]{"上海", "北京", "湖南", "湖北", "海南"}; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("简单列表对话框"); //千万不要加这句,不然列表显示不出来 // builder.setMessage("这是一个简单的列表对话框"); builder.setIcon(R.mipmap.ic_launcher); builder.setMultiChoiceItems(items, new boolean[]{false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { showText(items[which]); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText("确定"); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { showText("取消"); } }); builder.create().show(); }
进度条对话框
private void showProgressDialog() { final ProgressDialog dialog = new ProgressDialog(this); final int MAX_PROGRESS = 100; dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog.setCancelable(true); dialog.setCanceledOnTouchOutside(false); dialog.setIcon(R.mipmap.ic_launcher); dialog.setTitle("提示"); dialog.setMessage("这是一个进度条对话框"); dialog.setProgress(0); //监听取消时间 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { showText("进度条结束"); } }); dialog.show(); new Thread(new Runnable() { @Override public void run() { int progress = 0; while (progress < MAX_PROGRESS) { try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } progress++; //非UI线程,可以处理DialogUI dialog.setProgress(progress); } dialog.cancel(); } }).start(); }
编辑对话框
private void showEditDialog() { final EditText editText = new EditText(this); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.mipmap.ic_launcher); builder.setTitle("编辑对话框"); builder.setMessage("这是一个编辑对话框"); builder.setView(editText); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String input = editText.getText().toString(); showText(input); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); }
自定义对话框
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="299dip" android:layout_height="match_parent" android:background="@color/white" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="提示" android:layout_width="fill_parent" android:layout_height="40.0dip" android:gravity="center_vertical" android:paddingLeft="20.0dip" android:textSize="16dip" android:background="#288DF5" android:textColor="@color/white"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:gravity="left|center" android:paddingLeft="20dip" android:paddingRight="20dip" android:textColor="@color/black" android:text="@string/custom_dialog_msg"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dip"> <Button android:layout_width="fill_parent" android:layout_height="40dip" android:layout_weight="1" android:text="确定" android:textColor="@color/white" android:textSize="16sp"/> <Button android:layout_width="fill_parent" android:layout_height="40dip" android:layout_marginLeft="5dip" android:layout_weight="1" android:text="取消" android:textColor="@color/white" android:textSize="16sp"/> </LinearLayout> </LinearLayout> </LinearLayout>
private void showCustomDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.my_dialog, null); builder.setView(layout).create().show(); }
代码地址
相关文章推荐
- Android Dialog对话框的七种形式的使用
- Android开发中Dialog对话框的使用
- 经常使用的android弹出对话框
- android之对话框的使用
- Android时间选择器对话框的使用
- 经常使用的android弹出对话框
- Android开发中Dialog对话框的使用
- 【Android】Android对话框的使用
- Android中对话框的使用
- Android 对话框Dialog的使用,最基本,最基础的使用
- Android开发中Dialog对话框的使用
- Air for Android开发中使用Back键弹出是否退出对话框
- Android 对话框 Dialog使用实例讲解
- Android使用Activity用作弹出式对话框Dialog
- 【转】【Android】ProgressDialog进度条对话框的使用
- android—DatePicker 和TimePicker显示日期以及使用TimePickerDialog,DatePickerDialog来专门实现时间选择对话框
- Android系统搜索对话框(浮动搜索框)的使用 (转)
- Android开发基础之AlertDialog的单选对话框的使用
- C++ Builder下如何使用Android对话框
- Android中对话框(dialog)的使用