Android对话框
- 提示对话框AlertDialog(消息对话框,单选列表对话框,自定义对话框)
创建AlertDialog.Builder对象
- 自定义对话框
消息对话框,单选列表对话框,自定义对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.yongninggo.helloworld.MainActivity"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="对话框"
android:layout_marginTop="10dip"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框"
android:layout_marginTop="10dip"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义对话框"
android:layout_marginTop="10dip"/>
</LinearLayout>
activity.java文件
package com.yongninggo.helloworld;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Activity extends AppCompatActivity {
private Button btn1;
private Button btn2;
private Button btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setIcon(R.drawable.image1);
builder.setTitle("提示");
builder.setMessage("提示框内容");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您点击了确定",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setIcon(R.drawable.image1);
builder.setTitle("提示");
builder.setSingleChoiceItems(ma, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您选择了:"+ma[which],Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Activity.this,"您点击了"+ma[which],Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消",null);
builder.show();
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater.from(Activity.this);
View view = inflater.inflate(R.layout.activity1,null);
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setView(view);
builder.show();
}
});
}
}
- 进度条对话框(可见,不可见)ProgressDialog
ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialog
常用方法
- setProgressStyle:设置进度条风格,风格为圆形,旋转的。
- setTitlt:设置标题
- setMessage:设置提示信息;
- setIcon:设置标题图标;
- setIndeterminate:设置ProgressDialog的进度条是否不明确;这个属性对于ProgressDailog默认的转轮模式没有实际意义,默认下设置为true,它仅仅对带有ProgressBar的Dialog有作用。修改这个属性为false后可以实时更新进度条的进度。
- setMax(int)、getMax:设置最大进度条的值
- setProgress(int)、getProgress:更新进度条,当然一般都需要Handler的结合来更新进度条
1.直接创建
ProgressDialog pd = new ProgressDialog(this);
pd.show();
2.使用静态方式创建并显示,但是这种进度条只能是圆形条,可以设置title和Message提示内容
ProgressDialog pd1 = ProgressDialog.show(this, "提示", "正在登陆中");
3.使用静态方式创建并显示,这种进度条只能是圆形条,注意:这里最后一个参数boolean indeterminate设置是否是不明确的状态
ProgressDialog pd2 = ProgressDialog.show(this, "提示", "正在登陆中", false);
4.使用静态方式创建并显示,这种进度条只能是圆形条,注意:这里最后一个参数boolean cancelable 设置是否进度条是可以取消的
ProgressDialog pd3 = ProgressDialog.show(this, "提示", "正在登陆中", false, true);
使用静态方式创建并显示,但是这种进度条只能是圆形条,这里最后一个参数 DialogInterface.OnCancelListener(取消的监听)
private DialogInterface.OnCancelListener cancelListener;
cancelListener = new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "进度条被取消", Toast.LENGTH_LONG).show();
}
};
ProgressDialog pd4 = ProgressDialog.show(this, "提示", "正在登陆中", true, true, cancelListener);
圆形进度条
final ProgressDialog pd5 = new ProgressDialog(this);
pd5.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置进度条的形式为圆形转动的进度条
pd5.setCancelable(true);// 设置是否可以通过点击Back键取消
pd5.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
pd5.setIcon(R.mipmap.ic_launcher);//设置提示的title的图标,默认是没有的,如果没有设置title的话只设置Icon是不会显示图标的
pd5.setTitle("提示");
// dismiss监听
pd5.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
}
});
// 监听Key事件被传递给dialog
pd5.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return false;
}
});
// 监听cancel事件
pd5.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
}
});
//设置可点击的按钮,最多有三个(默认情况下)
pd5.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
pd5.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
pd5.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
pd5.setMessage("这是一个圆形进度条");
pd5.show();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
// cancel和dismiss方法本质都是一样的,都是从屏幕中删除Dialog,唯一的区别是
// 调用cancel方法会回调DialogInterface.OnCancelListener如果注册的话,dismiss方法不会回掉
pd5.cancel();
// dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
水平进度条
final ProgressDialog pd6 = new ProgressDialog(this);
pd6.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 设置水平进度条
pd6.setCancelable(true);// 设置是否可以通过点击Back键取消
pd6.setCanceledOnTouchOutside(false);// 设置在点击Dialog外是否取消Dialog进度条
pd6.setIcon(R.mipmap.ic_launcher);// 设置提示的title的图标,默认是没有的
pd6.setTitle("提示");
pd6.setMax(100);
pd6.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
pd6.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
pd6.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
pd6.setMessage("这是一个水平进度条");
pd6.show();
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while (i < 100) {
try {
Thread.sleep(200);
// 更新进度条的进度,可以在子线程中更新进度条进度
pd6.incrementProgressBy(1);
// dialog.incrementSecondaryProgressBy(10)//二级进度条更新方式
i++;
} catch (Exception e) {
}
}
// 在进度条走完时删除Dialog
pd6.dismiss();
}
}).start();
- 日期对话框DatePickDialog,时间对话框
时间对话框
1. 创建一个Fragment类继承DialogFragment,
2.实现onCreateDialog()方法返回一个TimePickerDialog实例
3. 实现TimePickerDialog.OnTimeSetListener的接口,用于监听用户时间的选择
package com.wuhr.com.dialogbgtransparent;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.widget.TimePicker;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by wuhr_pc on 2016-3-11.
*/
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(),this,hour,minute, android.text.format.DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
}
在主Activity调用显示时间选择器
DialogFragment dateDialog=new DatePickerFragment();
dateDialog.show(getSupportFragmentManager(),"timeDialog");
日期对话框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="false"
android:cursorVisible="false" />
<Button
android:text="日期对话框"
android:id="@+id/dateBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:text="时间对话框"
android:id="@+id/timeBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<DigitalClock
android:text="@+id/digitalClock"
android:textSize="20dip"
android:gravity="center"
android:id="@+id/DigitalClock01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<AnalogClock
android:id="@+id/analogClock"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
在Activity中调用的代码
package com.example.lyj.my;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity {
private Button dateBtn = null;
private Button timeBtn = null;
private EditText et=null;
private final static int DATE_DIALOG = 0;
private final static int TIME_DIALOG = 1;
private Calendar c = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.et);
dateBtn = (Button) findViewById(R.id.dateBtn);
timeBtn = (Button) findViewById(R.id.timeBtn);
dateBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
showDialog(DATE_DIALOG);
}
});
timeBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
showDialog(TIME_DIALOG);
}
});
} /* 创建日期及时间选择对话框 */
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case DATE_DIALOG:
c = Calendar.getInstance();
dialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {
et.setText("您选择了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日");
}
}, c.get(Calendar.YEAR), // 传入年份
c.get(Calendar.MONTH), // 传入月份
c.get(Calendar.DAY_OF_MONTH) // 传入天数
);
break;
case TIME_DIALOG:
c=Calendar.getInstance();
dialog=new TimePickerDialog(this,new TimePickerDialog.OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
et.setText("您选择了:"+hourOfDay+"时"+minute+"分");
}
},
c.get(Calendar.HOUR_OF_DAY),
c.get(Calendar.MINUTE),
false
);
break;
}
return dialog;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}