标签:
Android Dialog之 提示对话框,单选对话框,复选对话框,列表对话框,日期对话框,时间对话框,自定义对话框,以及进度对话框。
上代码:
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
import com.jbit.dialog_demo.R;
public class MainActivity extends Activity {
private Button btnAlert;
private Button btnList;
private Button btnCheckBox;
private Button btnRadio;
private Button btnProgress;
private Button btnDate;
private Button btnTime;
private Button btnUserDefined;
private ProgressDialog dialog;
private Dialog userDialog;
// 句柄
private Handler headler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAlert = (Button) findViewById(R.id.main_bt_alert_nom);
btnList = (Button) findViewById(R.id.main_bt_alert_list);
btnCheckBox = (Button) findViewById(R.id.main_bt_alert_checkbox);
btnRadio = (Button) findViewById(R.id.main_bt_alert_radio);
btnProgress = (Button) findViewById(R.id.main_bt_alert_poion);
btnDate = (Button) findViewById(R.id.main_bt_alert_date);
btnTime = (Button) findViewById(R.id.main_bt_alert_time);
btnUserDefined = (Button) findViewById(R.id.main_bt_alert_userdefinal);
btnAlert.setOnClickListener(listener);
btnList.setOnClickListener(listener);
btnCheckBox.setOnClickListener(listener);
btnRadio.setOnClickListener(listener);
btnProgress.setOnClickListener(listener);
btnDate.setOnClickListener(listener);
btnTime.setOnClickListener(listener);
btnUserDefined.setOnClickListener(listener);
// btnTime.setOnClickListener(this);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_bt_alert_nom:
showAlert();
break;
case R.id.main_bt_alert_checkbox:
showCheckBox();
break;
case R.id.main_bt_alert_date:
showDate();
break;
case R.id.main_bt_alert_list:
showList();
break;
case R.id.main_bt_alert_radio:
showRadio();
break;
case R.id.main_bt_alert_poion:
showPoion();
break;
case R.id.main_bt_alert_time:
showTime();
break;
case R.id.main_bt_alert_userdefinal:
showUserDefine();
break;
case R.id.dialog_btn_ok:
Toast.makeText(MainActivity.this, "你点击了确定按钮",
Toast.LENGTH_SHORT).show();
userDialog.dismiss();
break;
case R.id.dialog_btn_clean:
// 点击取消时关闭对话框
userDialog.dismiss();
break;
}
}
private void showUserDefine() {
// 获得加载视图的layout
LayoutInflater lif = LayoutInflater.from(MainActivity.this);
// 加载视图
View v = lif.inflate(R.layout.dialog_view, null);
// 为视图内的按钮添加事件操作
Button btnOk = (Button) v.findViewById(R.id.dialog_btn_ok);
Button btnClean = (Button) v.findViewById(R.id.dialog_btn_clean);
btnOk.setOnClickListener(listener);
btnClean.setOnClickListener(listener);
// 创建对话框
userDialog = new AlertDialog.Builder(MainActivity.this).setView(v)
.create();
// 设置点击边缘
userDialog.setCanceledOnTouchOutside(false);
// 显示对话框
userDialog.show();
}
private void showTime() {
TimePickerDialog dialog = new TimePickerDialog(MainActivity.this,
new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hour,
int minute) {
// TODO 自动生成的方法存根
}
}, Calendar.HOUR, Calendar.MINUTE, true);
dialog.show();
}
/**
* 进度条
*/
private void showPoion() {
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading...");
dialog.setButton(DialogInterface.BUTTON1, new String("取消"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
headler.removeCallbacks(r);
dialog.dismiss();
}
});
dialog.setMax(100);
dialog.setProgress(50);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
headler.postDelayed(r, 100);
}
private void showDate() {
Calendar calendar = Calendar.getInstance();
DatePickerDialog dia = new DatePickerDialog(MainActivity.this,
new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int month, int day) {
Toast.makeText(MainActivity.this, year + "-"
+ month + "-" + day, Toast.LENGTH_SHORT);
}
}, calendar.YEAR, calendar.MONTH, calendar.DAY_OF_MONTH);
dia.show();
}
private void showCheckBox() {
Dialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("多选对话框")
.setMultiChoiceItems(
new String[] { "item0", "item1", "item2" },
new boolean[] { true, false, false },
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(
DialogInterface dialogInterface,
int index, boolean isChecked) {
}
}).show();
}
private void showRadio() {
Dialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("单选对话框")
.setSingleChoiceItems(
new String[] { "item1", "item2", "item3" }, 0,
dialogClick).show();
}
private void showList() {
Dialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("列表对话框")
.setItems(new String[] { "item1", "item2", "item3" },
dialogClick).show();
}
private void showAlert() {
Dialog dialog = new AlertDialog.Builder(MainActivity.this)
.setIcon(R.drawable.ic_launcher).setMessage("这是一个提示框")
.setTitle("提示").setPositiveButton("取消", dialogClick)
.setNeutralButton("pass", dialogClick)
.setNegativeButton("ok", dialogClick).show();
}
private DialogInterface.OnClickListener dialogClick = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int index) {
// 显示一个对话框显示内容
// 第一个参数上下文
// 第二个参数提示的内容
// 第三个参数 显示的时间
Toast.makeText(MainActivity.this, String.valueOf(index),
Toast.LENGTH_LONG).show();
}
};
};
private Runnable r = new Runnable() {
@Override
public void run() {
if (dialog != null) {
if (dialog.getProgress() >= 100) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "卸载完成。。。",
Toast.LENGTH_SHORT).show();
return;
}
}
if (dialog != null) {
dialog.setProgress(dialog.getProgress() + 1);
}
headler.postDelayed(r, 100);
}
};
页面:
<?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"
>
<Button
android:id="@+id/main_bt_alert_nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提示对话框"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/main_bt_alert_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选对话框"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/main_bt_alert_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复选对话框"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/main_bt_alert_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="列表对话框"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/main_bt_alert_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="日期对话框"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/main_bt_alert_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间对话框"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/main_bt_alert_userdefinal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自定义对话框"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="@+id/main_bt_alert_poion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度条对话框"
android:textColor="#000000"
android:textSize="20sp" />
</LinearLayout>
}
标签:
原文地址:http://www.cnblogs.com/xinyigebi/p/xinyigebi.html