简介
提示框(Dialog)在手机APP中,是必不可少的,比如设置些简单的属性、进度提醒等。
就提示框(Dialog)而言,可以用两种方式:①、用android自带的类去实现;②、按自己的需要建一个xml作为一个对话框。
很明显第一种方法只能实现简单的对话框,代码量少;第二种方法可以实现任何想要的对话框,代码量大
第一种方式的常用简单的对话框
①普通对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("普通对话框").setMessage("是否删除?").setNegativeButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
arg0.dismiss();
}
})
.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this, "删除成功!",
Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
注:按钮分为确定按钮、取消按钮和中间按钮,当然中间按钮(setNeutralButton)没有在代码上体现。取消按钮一定在确定按钮的前面。
②列表对话框:

final String[] provices = new String[] { "北京", "湖南", "江苏", "上海", "浙江" };
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("选择省份:");
builder.setItems(provices, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
provice = provices[arg1];
Toast.makeText(MainActivity.this, "选择的城市为:" + provices[arg1],
Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
③单选对话框:

final String[] provices = new String[] { "北京", "湖南", "江苏", "上海", "浙江" };
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("单选按钮对话框");
builder.setSingleChoiceItems(provices, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,
"选择的城市是:" + provices[arg1], Toast.LENGTH_SHORT)
.show();
}
});
builder.create().show();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
④多选对话框:

final String[] provices = new String[] { "北京", "湖南", "江苏", "上海", "浙江" };
final boolean[] bs={false,false,false,false,false};
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("复选按钮对话框");
builder.setMultiChoiceItems(provices, bs,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1,
boolean arg2) {
}
});
builder.create().show();
第一种方式较复杂的对话框
⑤日期对话框

OnDateSetListener dateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
Toast.makeText(MainActivity.this,
"设置的时间为:" + arg1 + "年" + (arg2 + 1) + "月" + arg3 + "日",
Toast.LENGTH_SHORT).show();
}
};
DatePickerDialog datePickerDialog=new DatePickerDialog(this, dateSetListener, 2015, 10, 30);
datePickerDialog.show();
⑥进度条对话框

ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("进度条对话框");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setButton("Hide", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface Dialog, int which) {
}
});
progressDialog.setButton2("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
progressDialog.show();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
注:是不是发现有的dialog是先create然后又show的,其实不create是完全可以滴。。。
http://download.youkuaiyun.com/detail/zhengyikuangge/9311301 测试代码已上传,可能和上述代码有点差别,但大体上是一样一样的~
第二种方式实现步骤
案例示范:
①主xml文件添加一个按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="200dp"
android:layout_height="150dp"
>
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示框"
/>
</RelativeLayout>
②创建settishi.xml文件做对话框(只是简单的示范)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="150dp"
android:background="#FF0000">
</RelativeLayout>
③新建SetTiShi.java
public class SetTiShi extends Dialog {
Context context;
public SetTiShi(Context context, int theme) {
super(context, theme);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settishi);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
④res/values/styles.xml中添加代码
<style name="MyDialog" parent="@android:Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<!--读英文就知道啥意思了,把窗口背景改成无色 -->
</style>
⑤MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.bt1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
jianting();
}
});
}
public void jianting() {
Dialog dialog = new SetTiShi(this, R.style.MyDialog);
dialog.show();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
注:这种方法其实也不难,除去代码量的问题,它比第一种方法还简单。就是把一个普通的Activity变成了一个Dialog。这个就不加代码了~
附加第三种方式
Dialog mDialog = new Dialog(this);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable());
Window win = mDialog.getWindow();
win.setContentView(R.layout.set_ip_dialog);
mDialog.show();
改变Dialog出现的位置:
Dialog dialog = new Dialog(this)
Window dialogWindow = dialog.getWindow()
WindowManager.LayoutParams lp = dialogWindow.getAttributes()
lp.x = 100
lp.y = 100
dialogWindow.setAttributes(lp)
dialog.show()
注:另外说一下lp.x和lp.y具体怎么定位,和数学中坐标轴几乎一模一样,有四个象限,不过一二象限和三四象限换位置了,默认是在0,0显示,那就是屏幕中间。象限原点也可以移动。还有一点,要根据实际的Dialog样式来确定,多测试测试就好了。
注意在Dialog中的按钮添加监听事件时的写法和Activity中添加监听事件的区别(导入包的区别)
在声明一个Dialog对象也可以这样声明:
AlertDialog dialog = new AlertDialog.Builder(this).create();
给dialog中添加组件的时候有些不同:
添加Button时需要多加一个参数说明Button的类型,例如:
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "是",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
测试代码地址:
http://download.youkuaiyun.com/detail/zhengyikuangge/9509234