Android提示框Dialog
2015-10-19 19:19
441 查看
转载请标明出处:/article/8809947.html
本文出自【吴孝城的CSDN博客】
安卓中的提示框无非七种
下面是七种提示框的代码和效果图
两个选项的代码
![](http://img.blog.csdn.net/20151019194052379)
三个选项的代码
![](http://img.blog.csdn.net/20151019194108157)
带输入框的代码
![](http://img.blog.csdn.net/20151019194124180)
列表框代码
![](http://img.blog.csdn.net/20151019194143428)
单选框代码
![](http://img.blog.csdn.net/20151019194158565)
复选框代码
![](http://img.blog.csdn.net/20151019194315946)
自定义提示框的代码(一)
![](http://img.blog.csdn.net/20151019194330432)
自定义提示框的代码(二)
![](http://img.blog.csdn.net/20160115093424485)
下面是完整代码
activity_main.xml
自定义提示框的布局代码
custom.xml
Java代码如下
MainActivity.java
源码下载
本文出自【吴孝城的CSDN博客】
安卓中的提示框无非七种
下面是七种提示框的代码和效果图
两个选项的代码
//两个选项的点击功能的实现 //在不同版本中显示会有所不同,貌似从4.0开始,正的在右边。就是说确定按钮在右边 protected void one() { AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setIcon(R.mipmap.hhhh); builder.setTitle("两个选项"); builder.setMessage("关闭和取消"); builder.setNegativeButton("关闭", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MainActivity.this.finish(); } }); builder.setPositiveButton("取消", qxx); //点击取消按钮后执行qxx方法 builder.create().show(); //让对话框显示出来 }
三个选项的代码
//三个选项的实现 //在不同版本中显示会有所不同,貌似从4.0开始,正的在右边。就是说确定按钮在右边 protected void two() { Dialog dialog = new AlertDialog.Builder(this).setIcon( R.mipmap.hhhh).setTitle("三个选项").setMessage( "选择一个").setPositiveButton("第一", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了第一相", Toast.LENGTH_LONG).show(); } }).setNegativeButton("第二个", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了第二个", Toast.LENGTH_LONG).show(); } }).setNeutralButton("第三个", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了第三个", Toast.LENGTH_LONG).show(); } }).create(); dialog.show(); }
带输入框的代码
//带输入框的提示框 protected void three() { AlertDialog dialog = new AlertDialog.Builder(this).create(); dialog.setIcon(R.mipmap.hhhh); dialog.setTitle("提示内容"); dialog.setMessage("输入内容"); final EditText et = new EditText(this); dialog.setView(et); //确定 DialogInterface.OnClickListener ls = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你输入的内容是" + et.getText(), Toast.LENGTH_SHORT).show(); } }; dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", ls); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", qxx); dialog.show(); }
列表框代码
//列表框 //列表框不能设置setMessage(),不然不会显示列表内容 protected void four() { final String[] select = new String[]{"第一项", "第二项", "第三项", "第四项"}; Dialog alertDialog = new AlertDialog.Builder(this) .setIcon(R.mipmap.hhhh) .setTitle("选择一项") .setItems(select, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了" + select[which], Toast.LENGTH_SHORT).show(); } }). setNegativeButton("取消", qxx).create(); //点击取消按钮后执行qxx方法 alertDialog.show(); }
单选框代码
//单选框 //列表框不能设置setMessage(),不然不会显示单选内容 protected void five() { final String[] selects = new String[]{"选项一", "选项二", "选项三", "选项四"}; new AlertDialog.Builder(this).setIcon(R.mipmap.hhhh) .setTitle("单选框") //-1代表默认没选,0代表默认选择第一个 .setSingleChoiceItems(selects, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { bs = which; } }).setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了" + selects[bs], Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", qxx).create().show(); //点击取消按钮后执行qxx方法 }
复选框代码
//复选框 //列表框不能设置setMessage(),不然不会显示多选内容 protected void six() { final String item[] = new String[]{"第一个选项", "第二个选项", "第三个选项", "第四个选项"}; final boolean bselect[] = new boolean[item.length]; DialogInterface.OnMultiChoiceClickListener sl = new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //用数组记录下选择的所有项 bselect[which] = isChecked; } }; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMultiChoiceItems(item, null, sl); /* * builder.setMultiChoiceItems(item, null, sl)是一个多选, * 如果默认不选的话,直接设置为null,如果默认有选择的话 * 用new boolean[] {false,true,false,true} * 其中true为选择,false为不选 * 而且只能是boolean[] * 不能是Boolean[]*/ //点击确定后执行的内容 DialogInterface.OnClickListener ls = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String str = "你选择了:"; for (int i = 0; i < bselect.length; i++) { if (bselect[i]) { str = str + "\n" + item[i]; } } Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show(); } }; //点击确定按钮执行ls方法 builder.setPositiveButton("确定", ls); //点击取消按钮执行qxx方法 builder.setNegativeButton("取消", qxx); AlertDialog dialog = builder.create(); dialog.setIcon(R.mipmap.hhhh); dialog.setTitle("复选框"); dialog.show(); }
自定义提示框的代码(一)
//自定义对话框 protected void seven() { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom, null); AlertDialog dialog = new AlertDialog.Builder(this).create(); /*想完全自定义的话,这部分内容可不设置 dialog.setIcon(R.mipmap.hhhh); dialog.setTitle("自定义而已"); dialog.setMessage("输入帐户和密码");*/ dialog.setView(layout); //点击确定后执行的内容 DialogInterface.OnClickListener ls = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "确定提示", Toast.LENGTH_SHORT).show(); } }; //设置两个按钮 dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", ls); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", qxx); dialog.show(); }
自定义提示框的代码(二)
private void custom() { new AlertDialog.Builder(this) .setIcon(R.drawable.hhhh) .setTitle("菜单") .setItems(new String[] { "一", "二", "三" }, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: Toast.makeText(MainActivity.this, "一", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MainActivity.this, "二", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MainActivity.this, "三", Toast.LENGTH_SHORT).show(); break; } } }).show(); }
下面是完整代码
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal"> <Button android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="两个选项"/> <Button android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="三个选项"/> <Button android:id="@+id/three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="输入框"/> <Button android:id="@+id/four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="例表"/> <Button android:id="@+id/five" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单选"/> <Button android:id="@+id/six" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="多选"/> <Button android:id="@+id/seven" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="自定义"/> </LinearLayout>
自定义提示框的布局代码
custom.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/hhhh"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#22B8DD"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="帐户:" android:textSize="28sp"/> <EditText android:id="@+id/name" android:layout_width="180dp" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码:" android:textSize="28sp"/> <EditText android:id="@+id/password" android:layout_width="180dp" android:inputType="textPassword" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
Java代码如下
MainActivity.java
package cn.wuxiaocheng.dialog;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 MainActivity extends AppCompatActivity {
private int bs = 0; //单选
//用数组定义七个按钮
private int[] button = {R.id.one, R.id.two, R.id.three, R.id.four, R.id.five, R.id.six, R.id.seven};
private Button[] buttons = new Button[button.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//数组找到按钮和设置监听
ChooseBtn btn = new ChooseBtn();
for (int i = 0; i < button.length; i++) {
buttons[i] = (Button) findViewById(button[i]);
buttons[i].setOnClickListener(btn);
}
}
//内部类对按钮点击事件
class ChooseBtn implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.one:
one();
break;
case R.id.two:
two();
break;
case R.id.three:
three();
break;
case R.id.four:
four();
break;
case R.id.five:
five();
break;
case R.id.six:
six();
break;
case R.id.seven:
seven();
break;
}
}
}
//两个选项的点击功能的实现 //在不同版本中显示会有所不同,貌似从4.0开始,正的在右边。就是说确定按钮在右边 protected void one() { AlertDialog.Builder builder = new Builder(MainActivity.this); builder.setIcon(R.mipmap.hhhh); builder.setTitle("两个选项"); builder.setMessage("关闭和取消"); builder.setNegativeButton("关闭", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { MainActivity.this.finish(); } }); builder.setPositiveButton("取消", qxx); //点击取消按钮后执行qxx方法 builder.create().show(); //让对话框显示出来 }
//三个选项的实现 //在不同版本中显示会有所不同,貌似从4.0开始,正的在右边。就是说确定按钮在右边 protected void two() { Dialog dialog = new AlertDialog.Builder(this).setIcon( R.mipmap.hhhh).setTitle("三个选项").setMessage( "选择一个").setPositiveButton("第一", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了第一相", Toast.LENGTH_LONG).show(); } }).setNegativeButton("第二个", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了第二个", Toast.LENGTH_LONG).show(); } }).setNeutralButton("第三个", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了第三个", Toast.LENGTH_LONG).show(); } }).create(); dialog.show(); }
//带输入框的提示框
protected void three() {
AlertDialog dialog = new AlertDialog.Builder(this).create();
dialog.setIcon(R.mipmap.hhhh);
dialog.setTitle("提示内容");
dialog.setMessage("输入内容");
final EditText et = new EditText(this);
dialog.setView(et);
//确定
DialogInterface.OnClickListener ls = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//.toString()是把输入的内容转化为字符串,.trim()是将字符串头部和尾部的空白去掉
Toast.makeText(MainActivity.this, "你输入的内容是" + et.getText().toString().trim(), Toast.LENGTH_SHORT).show();
}
};
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", ls);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", qxx);
dialog.show();
}
//列表框 //列表框不能设置setMessage(),不然不会显示列表内容 protected void four() { final String[] select = new String[]{"第一项", "第二项", "第三项", "第四项"}; Dialog alertDialog = new AlertDialog.Builder(this) .setIcon(R.mipmap.hhhh) .setTitle("选择一项") .setItems(select, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择了" + select[which], Toast.LENGTH_SHORT).show(); } }). setNegativeButton("取消", qxx).create(); //点击取消按钮后执行qxx方法 alertDialog.show(); }
//单选框
//列表框不能设置setMessage(),不然不会显示单选内容
protected void five() {
final String[] selects = new String[]{"选项一", "选项二", "选项三", "选项四"};
new AlertDialog.Builder(this).setIcon(R.mipmap.hhhh)
.setTitle("单选框")
//-1代表默认没选,0代表默认选择第一个
.setSingleChoiceItems(selects, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
bs = which;
}
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你选择了" + selects[bs], Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", qxx).create().show(); //点击取消按钮后执行qxx方法
}
//复选框 //列表框不能设置setMessage(),不然不会显示多选内容 protected void six() { final String item[] = new String[]{"第一个选项", "第二个选项", "第三个选项", "第四个选项"}; final boolean bselect[] = new boolean[item.length]; DialogInterface.OnMultiChoiceClickListener sl = new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //用数组记录下选择的所有项 bselect[which] = isChecked; } }; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMultiChoiceItems(item, null, sl); /* * builder.setMultiChoiceItems(item, null, sl)是一个多选, * 如果默认不选的话,直接设置为null,如果默认有选择的话 * 用new boolean[] {false,true,false,true} * 其中true为选择,false为不选 * 而且只能是boolean[] * 不能是Boolean[]*/ //点击确定后执行的内容 DialogInterface.OnClickListener ls = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String str = "你选择了:"; for (int i = 0; i < bselect.length; i++) { if (bselect[i]) { str = str + "\n" + item[i]; } } Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show(); } }; //点击确定按钮执行ls方法 builder.setPositiveButton("确定", ls); //点击取消按钮执行qxx方法 builder.setNegativeButton("取消", qxx); AlertDialog dialog = builder.create(); dialog.setIcon(R.mipmap.hhhh); dialog.setTitle("复选框"); dialog.show(); }
//自定义对话框
protected void seven() {
//布局解析器
LayoutInflater inflater = getLayoutInflater();
//用布解析器拿到布局
View layout = inflater.inflate(R.layout.custom, null);
AlertDialog dialog = new AlertDialog.Builder(this).create();
/*想完全自定义的话,这部分内容可不设置
dialog.setIcon(R.mipmap.hhhh);
dialog.setTitle("自定义而已");
dialog.setMessage("输入帐户和密码");*/
dialog.setView(layout);
//点击确定后执行的内容
DialogInterface.OnClickListener ls = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定提示", Toast.LENGTH_SHORT).show();
}
};
//设置两个按钮
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", ls);
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", qxx);
dialog.show();
}
//为提高代码复用性,所有取消按钮执行的方法都在这里,当点击取消按钮时,关闭提示框
DialogInterface.OnClickListener qxx = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
};
}
源码下载
相关文章推荐
- 登录的意义
- Android PopupWindow实现从顶部弹出下拉菜单左、中、右
- Android studio Gradle home can not be found - Android Studio错误
- Android Camera 使用小结
- [Android应用开发] 04.页面跳转和数据传输
- Android SQLite 的学习
- Android中帧布局-FrameLayout和网格布局-GridLayout
- Android的使用介绍
- Android ViewPager屏蔽预加载
- Android permission 访问权限大全
- Android一百个错误整理(一)
- 使用Android Studio向GitHub提交代码
- 使用Android Studio向GitHub提交代码
- Android第一周-项目下各文件夹的作用
- AndroidWear开发之HelloWorld篇
- AndroidWear开发之开发环境[前奏]
- ExpandableListView的用法(类似QQ好友展示)
- 底层 添加资源到android源码中-修改framework层
- Android6.0给开发者带来哪些影响
- Support Design Library 之 TabLayout 使用与分析