Android几大对话框的使用:普通对话框,自定义对话框,圆形进度条对话框,线性进度条对话框,日历对话框,时间对话框
AlertDialog类中有一个静态内部类Builder,通过AlertDialog.Builder来调用,从而实现对话框的使用,在此之前,我们先创一个带有单击事件的按钮实现对话框:
在activity页面中,通过Button按钮的单击事件来实现对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(AlertActivity.this);
builder.setTitle("退出提示");
builder.setMessage("确定要退出程序吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertActivity.this, "孩子,你点的是确定", Toast.LENGTH_SHORT).show();
finish();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertActivity.this, "欢迎留下", Toast.LENGTH_SHORT).show();
}
});
builder.show();
与普通对话框相比较而言,自定义对话框需要自己定义一个layout布局文件,作为对话框的布局,附上布局文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="请输入用户名和密码"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/alert_et1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:" />
<EditText
android:id="@+id/alert_et2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LinearLayout>
LinearLayout>
通过Button按钮来实现单击事件:
同样,在activity页面用Button按钮的单机事件来实现:
View view = LayoutInflater.from(this).inflate(R.layout.alert_item, null);
et1 = (EditText) view.findViewById(R.id.alert_et1);
et2 = (EditText) view.findViewById(R.id.alert_et2);
AlertDialog.Builder builder1 = new AlertDialog.Builder(AlertActivity.this);
builder1.setTitle("温馨提示");
builder1.setIcon(R.mipmap.a);
builder1.setView(view);
builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertActivity.this, et1.getText().toString()+"\n"+et2.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(AlertActivity.this, "别看了,你啥都没填", Toast.LENGTH_SHORT).show();
}
});
builder1.show();
调用方法上来看与普通对话框的调用一样,不过需要注意,因为是自定义,所以必须使用自己定义的布局文件
使用ProgressDialog来使用此类的对话框,同样是Button按钮来调用单击事件,这里就直接写activity中等java代码,首先,定义一个ProgressDialog
private ProgressDialog pd;
以下是完整代码:
pd=new ProgressDialog(AlertActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setTitle("提示");
pd.setMessage("这是一个圆形对话框进度条");
pd.setIcon(R.mipmap.b);
pd.setIndeterminate(false);
pd.setCancelable(false);
pd.setButton(-1,"确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
pd.show();
与圆形进度条对话框的调用方法基本一样,不过要注意,进度条的进度需要开启一个线程才能有进度:
pd=new ProgressDialog(AlertActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
pd.setIcon(R.mipmap.c);
pd.setMessage("这是线形进度条对话框");
pd.setTitle("呼呼");
pd.setCancelable(false);
pd.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
pd.show();
下面要使进度条读条,开启一个新的线程:
new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<=100;i++){
pd.setProgress(i);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
这个可以调用系统的方法,方法也比较固定,代码如下:
Calendar calendar=Calendar.getInstance();
DatePickerDialog datePickerDialog=new DatePickerDialog(AlertActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(AlertActivity.this, "所选的日期是:"+year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_LONG).show();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
Calendar calendar1=Calendar.getInstance();
final TimePickerDialog time=new TimePickerDialog(AlertActivity.this,new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(AlertActivity.this, "当前时间:"+hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
btn6.setText(hourOfDay+":"+minute);
}
},11,calendar1.get(Calendar.MINUTE),true);
time.show();