简介
提示框(Dialog)在手机APP中,是必不可少的,比如设置些简单的属性、进度提醒等。
就提示框(Dialog)而言,可以用两种方式:①、用android自带的类去实现;②、按自己的需要建一个xml作为一个对话框。
很明显第一种方法只能实现简单的对话框android进度条对话框,代码量少;第二种方法可以实现任何想要的对话框,代码量大
第一种方式的常用简单的对话框 ①普通对话框:
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是完全可以滴。。。
测试代码已上传,可能和上述代码有点差别,但大体上是一样一样的~
第二种方式实现步骤
案例示范:
①主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文件做对话框(只是简单的示范)
<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中添加代码