时间:2021-05-20
在开发过程中,与用户交互式免不了会用到对话框以实现更好的用户体验,所以掌握几种对话框的实现方法还是非常有必要的。在看具体实例之前先对AlertDialog做一个简单介绍。AlertDialog是功能最丰富、实践应用最广的对话框,它可以生成各种内容的对话框。但实际上AlertDialog生成的对话框总体可分为以下4个区域:图标区、标题区、内容区、按钮区。
这里总结了九种对话框的实现方法,有需要的朋友可以来学习下了
除了popupwindow实现稍微麻烦一点,其他形似都相对简单,熟悉2便即可
直接上源码
package com.naoh.stu; import java.util.ArrayList; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.PopupWindow; import android.widget.Toast; public class DiaAllActivity extends Activity implements Runnable { 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 Button btn_diaCustom; private Button btn_popUpDia; private PopupWindow window=null; private Button cusPopupBtn1; private View popupView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog); 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); btn_diaCustom=(Button)findViewById(R.id.btn_diaCustom); btn_popUpDia=(Button)findViewById(R.id.btn_popUpDia); } 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); btn_diaCustom.setOnClickListener(btnListener); btn_popUpDia.setOnClickListener(btnListener); } 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; case R.id.btn_diaCustom: showCustomDia(); break; case R.id.btn_popUpDia: showCusPopUp(v); break; default: break; } } } }; private void showNormalDia() { //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext()); AlertDialog.Builder normalDia=new AlertDialog.Builder(DiaAllActivity.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(DiaAllActivity.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","选项4","选项5","选项6","选项7"}; AlertDialog.Builder listDia=new AlertDialog.Builder(DiaAllActivity.this); listDia.setTitle("列表对话框"); listDia.setItems(mList, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub showClickMessage(mList[which]); } }); listDia.create().show(); } int yourChose=-1; private void showSinChosDia() { final String[] mList={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"}; yourChose=-1; AlertDialog.Builder sinChosDia=new AlertDialog.Builder(DiaAllActivity.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(); } ArrayList<Integer> myChose= new ArrayList<Integer>(); private void showMultiChosDia() { final String[] mList={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"}; final boolean mChoseSts[]={false,false,false,false,false,false,false}; myChose.clear(); AlertDialog.Builder multiChosDia=new AlertDialog.Builder(DiaAllActivity.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(); } //进度读取框需要模拟读取 ProgressDialog mReadProcessDia=null; public final static int MAX_READPROCESS = 100; private void showReadProcess() { mReadProcessDia=new ProgressDialog(DiaAllActivity.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(DiaAllActivity.this); processDia.setTitle("进度条框"); processDia.setMessage("内容读取中..."); processDia.setIndeterminate(true); processDia.setCancelable(true); processDia.show(); } private void showCustomDia() { AlertDialog.Builder customDia=new AlertDialog.Builder(DiaAllActivity.this); final View viewDia=LayoutInflater.from(DiaAllActivity.this).inflate(R.layout.custom_dialog, null); customDia.setTitle("自定义对话框"); customDia.setView(viewDia); customDia.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub EditText diaInput=(EditText) viewDia.findViewById(R.id.txt_cusDiaInput); showClickMessage(diaInput.getText().toString()); } }); customDia.create().show(); } private void showCusPopUp(View parent) { if(window == null) { popupView=LayoutInflater.from(DiaAllActivity.this).inflate(R.layout.dia_cuspopup_dia, null); cusPopupBtn1=(Button)popupView.findViewById(R.id.diaCusPopupSure); window =new PopupWindow(popupView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT); } window.setAnimationStyle(R.style.PopupAnimation); window.setFocusable(true); window.setBackgroundDrawable(new BitmapDrawable()); window.update(); window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0); cusPopupBtn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showClickMessage("popup window的确定"); } }); } private void showClickMessage(String message) { Toast.makeText(DiaAllActivity.this, "你选择的是: "+message, Toast.LENGTH_SHORT).show(); } }布局,就是一堆的button
<?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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="普通Dialog" android:id="@+id/btn_diaNormal"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多按钮Dialog" android:id="@+id/btn_diaMulti"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="列表Dialog" android:id="@+id/btn_diaList"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="单项选择Dialog" android:id="@+id/btn_diaSigChos"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多项选择Dialog" android:id="@+id/btn_diaMultiChos"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="进度条Dialog" android:id="@+id/btn_diaReadProcess"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="读取中Dialog" android:id="@+id/btn_diaProcess"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义Dialog" android:id="@+id/btn_diaCustom"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="PopUpWindow实现的dialog" android:id="@+id/btn_popUpDia"/> </LinearLayout>以上是Android开发必知 九种对话框的实现方法,代码主要用AlertDialog生成的对话框,它的功能丰富、使用最广。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
android开发中实现单选与多选对话框的代码非常简单,具体代码如下所示:publicvoidmyClick(Viewview){//单选对话框//single
本文实例讲述了Android编程实现AlertDialog自定义弹出对话框的方法。分享给大家供大家参考,具体如下:弹出对话框,显示自定义的布局文件弹出对话框提示
Android中Popwindow弹出菜单的两种方法实例1.popWindow就是对话框的一种方式!此文讲解的android中对话框的一种使用方式,它叫popW
本文实例讲述了Android开发之PopupWindow创建弹窗、对话框的方法。分享给大家供大家参考,具体如下:简介:PopupWindow可创建类似对话框风格
下面通过实例代码给大家分享5种android对话框,具体内容详情如下所示:1弹出普通对话框---系统更新2自定义对话框--用户登录3时间选择对话框--时间对话框