android Dialog 各种Dialog 三个选项的、进度条的、单选的、多选的、自定义的对话框
16lz
2021-01-25
文字就不说了,直接上代码。
1.三个选项Dialog.
效果图如下:
/** * 三个选项的Dialog */ private void threeDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("带进度条的Dialog"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();}}); builder.setNeutralButton("中间", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(mContext, "中间", Toast.LENGTH_SHORT).show();}}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();}}); builder.create().show(); }
二、进度条Dialog。
代码:
/** * 进度条Dialog */ private void progressDialog(){ mProgress = new ProgressDialog(mContext); mProgress.setIcon(R.drawable.ic_launcher); mProgress.setTitle("带进度条的Dialog"); mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgress.setButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();}}); mProgress.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show(); } }); mProgress.show(); new Thread(new Runnable() {int progress = 0;@Overridepublic void run() {// TODO Auto-generated method stubwhile (progress <= 100) {mProgress.setProgress(progress);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}progress++;}}}).start(); }
三、单选Dialog
/** * 单选Dialog */ private void simpleDialog(){ singleSelectedId = -1; AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("单选Dialog"); builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsingleSelectedId = which;Toast.makeText(mContext, "你选择的ID为:"+which, Toast.LENGTH_SHORT).show();}}); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubif (singleSelectedId >= 0) {Toast.makeText(mContext, "你选择的ID为:"+singleSelectedId, Toast.LENGTH_SHORT).show();} else {singleSelectedId = 0;// 业务逻辑}}}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}); builder.create().show(); }
四、多选复选Dialog
/** * 复选Dialog */ private void chooseDialog(){ boolean[] selected = new boolean[items.length]; for (int i = 0; i < selected.length; i++) {selected[i] = false;} AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("复选Dialog"); builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() { @Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {list.add(which);} else {list.remove(which);}}}); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {String str = "你选择了:";@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubfor (int i = 0; i < list.size(); i++) {str += " "+items[list.get(i)];}Toast.makeText(mContext, str, Toast.LENGTH_LONG).show();}}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}); builder.create().show(); }
五、自定义Dialog
/** * 自定义Dialog */ EditText username; EditText password; private void customDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(mContext); LayoutInflater flater = LayoutInflater.from(mContext); final View view = flater.inflate(R.layout.login, null); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("自定义Dialog"); builder.setView(view); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubusername = (EditText) view.findViewById(R.id.username);password = (EditText) view.findViewById(R.id.pwd);Toast.makeText(mContext, ""+username.getText()+password.getText(), Toast.LENGTH_LONG).show();}}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}); builder.create().show(); }
整体代码如下:
HelloDialogActivity.java:
package com.covics.zfh;import java.util.ArrayList;import android.app.Activity;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.text.style.BulletSpan;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class HelloDialogActivity extends Activity implements OnClickListener {private Button btnThree;private Button btnProgress;private Button btnSingle;private Button btnChoices;private Button btnCustom;private Context mContext;private ProgressDialog mProgress;private int singleSelectedId; // 单选IDprivate String[] items = {"item0", "item1", "item2", "item3", "item4", "item5"};private ArrayList<Integer> list; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mContext = HelloDialogActivity.this; prepare(); } private void prepare(){ btnThree = (Button)findViewById(R.id.btn_three); btnProgress = (Button)findViewById(R.id.btn_progress); btnSingle = (Button)findViewById(R.id.btn_dan); btnChoices = (Button)findViewById(R.id.btn_mus); btnCustom = (Button)findViewById(R.id.btn_custom); btnThree.setOnClickListener(this); btnProgress.setOnClickListener(this); btnSingle.setOnClickListener(this); btnChoices.setOnClickListener(this); btnCustom.setOnClickListener(this); } /** * 三个选项的Dialog */ private void threeDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("带进度条的Dialog"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();}}); builder.setNeutralButton("中间", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(mContext, "中间", Toast.LENGTH_SHORT).show();}}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();}}); builder.create().show(); } /** * 进度条Dialog */ private void progressDialog(){ mProgress = new ProgressDialog(mContext); mProgress.setIcon(R.drawable.ic_launcher); mProgress.setTitle("带进度条的Dialog"); mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgress.setButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubToast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();}}); mProgress.setButton2("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show(); } }); mProgress.show(); new Thread(new Runnable() {int progress = 0;@Overridepublic void run() {// TODO Auto-generated method stubwhile (progress <= 100) {mProgress.setProgress(progress);try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}progress++;}}}).start(); } /** * 单选Dialog */ private void simpleDialog(){ singleSelectedId = -1; AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("单选Dialog"); builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubsingleSelectedId = which;Toast.makeText(mContext, "你选择的ID为:"+which, Toast.LENGTH_SHORT).show();}}); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubif (singleSelectedId >= 0) {Toast.makeText(mContext, "你选择的ID为:"+singleSelectedId, Toast.LENGTH_SHORT).show();} else {singleSelectedId = 0;// 业务逻辑}}}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}); builder.create().show(); } /** * 复选Dialog */ private void chooseDialog(){ boolean[] selected = new boolean[items.length]; for (int i = 0; i < selected.length; i++) {selected[i] = false;} AlertDialog.Builder builder = new AlertDialog.Builder(mContext); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("复选Dialog"); builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() { @Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubif (isChecked) {list.add(which);} else {list.remove(which);}}}); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {String str = "你选择了:";@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubfor (int i = 0; i < list.size(); i++) {str += " "+items[list.get(i)];}Toast.makeText(mContext, str, Toast.LENGTH_LONG).show();}}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}); builder.create().show(); } /** * 自定义Dialog */ EditText username; EditText password; private void customDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(mContext); LayoutInflater flater = LayoutInflater.from(mContext); final View view = flater.inflate(R.layout.login, null); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("自定义Dialog"); builder.setView(view); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubusername = (EditText) view.findViewById(R.id.username);password = (EditText) view.findViewById(R.id.pwd);Toast.makeText(mContext, ""+username.getText()+password.getText(), Toast.LENGTH_LONG).show();}}); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stub}}); builder.create().show(); }@Overridepublic void onClick(View v) {int id = v.getId();switch (id) {case R.id.btn_three : { threeDialog(); break;}case R.id.btn_progress : {progressDialog();break;}case R.id.btn_dan: {simpleDialog();break;}case R.id.btn_mus : { list = new ArrayList<Integer>();chooseDialog();break;}case R.id.btn_custom : {customDialog();break;}default :break;}}}
layout中文件:
main.xml
<?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:id="@+id/btn_three" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="三个选项的Dialog" /><Button android:id="@+id/btn_progress" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="进度条Dialog" /><Button android:id="@+id/btn_dan" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="单选Dialog" /><Button android:id="@+id/btn_mus" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多选Dialog" /><Button android:id="@+id/btn_custom" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义Dialog" /></LinearLayout>
login.xml:
<?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" > <LinearLayout android:id="@+id/user_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/text_user" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" /> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" android:saveEnabled="true" android:hint="姓名" /> </LinearLayout> <LinearLayout android:id="@+id/pwd_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/text_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" /> <EditText android:id="@+id/pwd" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:saveEnabled="true" android:hint="密码" /> </LinearLayout> </LinearLayout>
祝大家学习Android愉快!
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- Android(安卓)学习笔记 databinding简单使用:使用databinding在li
- android通过代码判断手机是否root
- Android模块化编程——WebView使用之与JavaScript交互
- Android(安卓)IllegalArgumentException: Cannot draw recycled
- Android(安卓)签名打包出现的错误的解决方法以及代码中获取应用
- 关于android camera2 图像格式的一些问题
- Android面试-Android项目构建
随机推荐
- Android-Framework: Activity、Window、V
- windows android ndk环境搭建
- adt-bundle-mac 配置Cocos2d-x 注意事项
- 学习笔记 Android(安卓)LocalBroadcastMa
- Android(安卓)Thread interrupt 中断JAVA
- 详解Android(安卓)9.0 私有API禁用机制
- Android(安卓)Studio 关于高德地图的开发
- Android(安卓)内存泄漏检查工具LeakCanar
- Android隐藏输入法键盘(hideSoftInputFro
- Android(安卓)Activity和Intent机制学习