/**
* 测试对话框
* @author Administrator
*
*/
public class DialogActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
//显示一般AlertDialog
public void shuowAD(View v){
new AlertDialog.Builder(this)
.setTitle("删除数据")//设置标题
.setMessage("你确定要删除数据马?")//设置提示的具体信息
.setPositiveButton("删除",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "删除数据", Toast.LENGTH_SHORT).show();
}
})
//设置负面按钮,一般是取消的操作
.setNegativeButton("取消", new OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "取消删除数据", Toast.LENGTH_SHORT).show();
}
})
//显示对话框
.show();
}
//显示单选列表AlertDialog
public void showLD(View v){
final String[] items = {"红","蓝","绿","灰"};
new AlertDialog.Builder(this)
.setTitle("指定背景颜色")//设置标题
//设置单选列表(参数1列表项内容,2单前选中项,3监听器)
.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// which就是选择的下标
Toast.makeText(DialogActivity.this, items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
})
//显示对话框
.show();
}
//显示自定义的AlertDialog
public void showDIY(View v){
//动态加载布局文件,得到对应的View对象
View view = View.inflate(this, R.layout.alertdialog_diy, null);
/**
* Q1:view的真实类型是?布局文件根标签的类型,包含了子View对象
*
* Q2:如何得到一个独立的View的子view?view.findViewById
* findViewById是在setContentView()加载的布局中查找
*/
final EditText nameET = (EditText) view.findViewById(R.id.et_dialog_name);
final EditText pwdET = (EditText) view.findViewById(R.id.et_dialog_pwd);
new AlertDialog.Builder(this)
.setView(view)//设置自定义布局
.setNegativeButton("取消", null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//读取用户名和密码
String username = nameET.getText().toString().trim();
String pwd = pwdET.getText().toString().trim();
//提示
Toast.makeText(DialogActivity.this, username + "\n" + pwd, 3).show();
}
})
.show();
}
//显示圆形进度ProgressDialog
public void showPD(View v){//回调方法:都在主线程执行
final ProgressDialog dialog = ProgressDialog.show(this, "数据加载", "数据加载中... ...");
//模拟做了一个长时间的工作
//长时间的工作不能在主线程做,需启动分线程完成
new Thread(){
public void run(){
for (int i = 0; i < 20; i++) {
//休息一会儿
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//移除Dialog
dialog.dismiss();//方法在分线程里执行,但内部使用Handler实现主线程移除Dialog
//不能在分线程里直接更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
//显示Toast
Toast.makeText(DialogActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
}
});
};
}.start();
}
//显示水平进度ProgressDialog
public void showSP(View v){
//1.创建Dialog对象
final ProgressDialog pd = new ProgressDialog(this);
//2.设置样式(水平进度条样式)
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//显示
pd.show();
//启动分线程加载数据,并显示及进度,当加载完成移除Dialog
new Thread(new Runnable() {
@Override
public void run() {//分线程执行的代码
int count = 20;
//设置最大进度
pd.setMax(count);
//耗时的操作
for (int i = 0; i < count; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
pd.setProgress(pd.getProgress() + 1);
}
//移除Dialog
pd.dismiss();
//不能在分线程里直接更新UI
runOnUiThread(new Runnable() {
@Override
public void run() {
//显示Toast
Toast.makeText(DialogActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
}
});
}
}).start();
}
//显示日历的对话框DatePickerDialog
public void showDPD(View v){
//创建日历对象
Calendar calender = Calendar.getInstance();
//得到当前的年月日
int year = calender.get(Calendar.YEAR);
int monthOfYear = calender.get(Calendar.MONTH);
int dayOfMonth = calender.get(Calendar.DAY_OF_MONTH);
Log.e("TAG", year + "-" + monthOfYear + "-" + dayOfMonth);
//创建日期对话框(参数1.上下文,2.点完成按钮后的操作,3.年,4.月,5.日)
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Log.e("TAG", year + "--" + monthOfYear + "--" + dayOfMonth);
}
}, year, monthOfYear, dayOfMonth).show();
}
//显示时间的对话框TimePickerDialog
public void showTPD(View v){
//创建时间对象
Calendar calender = Calendar.getInstance();
//得到当前的小时,分
int hourOfDay = calender.get(Calendar.HOUR_OF_DAY);
int minute = calender.get(Calendar.MINUTE);
Log.e("TAG", hourOfDay + ":" + minute);
//创建时间对话框(参数1.上下文,2.点完成按钮后的操作,3.小时,4.分,5.是否使用24小时格式)
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Log.e("TAG", hourOfDay + "::" + minute);
}
}, hourOfDay, minute, true).show();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="showAD"
android:text="显示一般AlertDialog" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:onClick="showLD"
android:text="显示单选列表AlertDialog" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button2"
android:onClick="showDIY"
android:text="显示自定义Alertlog" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button3"
android:onClick="showPD"
android:text="显示圆形进度ProgressDialog" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button4"
android:onClick="showSP"
android:text="显示水平进度ProgressDialog" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button5"
android:onClick="showDPD"
android:text="显示DataPickerDialog" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button6"
android:onClick="showTPD"
android:text="显示TimePickerDialog" />
</RelativeLayout>
<?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" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/o_li" />
<EditText
android:id="@+id/et_dialog_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/et_dialog_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"/>
</LinearLayout>