赞
踩
Dialog是一种能遮盖住其他控件操作特殊控件,一般用于确认用户操作或者有紧急的信息需要回馈给用户,会需要使用到Dialog,下面是一些关于Dialog的使用。
- package com.example.dialogdemo;
-
- import java.util.Calendar;
-
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.DatePicker;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.TimePicker;
- import android.widget.Toast;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.DatePickerDialog;
- import android.app.DatePickerDialog.OnDateSetListener;
- import android.app.ProgressDialog;
- import android.app.TimePickerDialog;
- import android.app.TimePickerDialog.OnTimeSetListener;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.DialogInterface.OnMultiChoiceClickListener;
-
- public class MainActivity extends Activity implements OnClickListener, Runnable {
- ProgressDialog pDialog;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView tv = (TextView) findViewById(R.id.dailog_tv);
- tv.setOnClickListener(this);
- }
-
- /**
- * AlertDialog
- */
- // 消息对话框
- public void messageDialog() {
- // 提示对话框的建立者,新建AlertDialog内部类Buileder的对象
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- // Builder builder = new Builder(this);//也可以直接new Bulider对象
- // 设置标题,没有标题就没有图标
- builder.setTitle("消息");
- // 设置图标
- builder.setIcon(R.drawable.ic_launcher);
- // 设置对话框点击其他位置不会被取消
- builder.setCancelable(false);
- // 设置左边按键。4.0以后消极在左边。Negative
- builder.setNegativeButton("消极", new DialogInterface.OnClickListener() {
-
- /**
- * @parm dialog-->当前按钮所在的对话框
- * @parm which-->判断是那个按键,因为监听的只是消极,所以在这没用 如果设置了两个一样的 按键,会被后一个覆盖
- */
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- // 设置右边按键,Positive
- builder.setPositiveButton("积极", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- // 设置中间按键,Neutral
- builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- });
-
- // 设置消息
- builder.setMessage("天降正义");
- // 通过builder.create(),新建AlertDialog对象
- AlertDialog dialog = builder.create();
- dialog.show();
- }
-
- // 列表对话框
- public void itemDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("列表");
-
- final String[] items = { "呵呵", "哈哈", "嘻嘻", "dj", "ds", "sd" };
- // 设置列表
- builder.setItems(items, new DialogInterface.OnClickListener() {
-
- /**
- * @parm which-->当前所选中的元素
- */
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Log.e("items[" + which + "]", items[which]);
- }
- });
- AlertDialog dialog = builder.create();
- dialog.show();
- }
-
- // 单选对话框
- public void singleDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("单选");
- // 单选元素
- final String[] items = { "呵呵", "哈哈", "嘻嘻", "dj", "ds", "sd" };
- // 设置单选元素
- /*
- * items单选的元素, checkedItem选中的元素的角标,默认选中的元素的角标,若要设置无选中则设置为-1
- */
- builder.setSingleChoiceItems(items, -1,
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Log.e("items[" + which + "]", items[which]);
- }
- });
- // 包含创建对话框和显示对话框,可以不用创建Dialog对象
- builder.show();
- }
-
- // 多选对话框
- public void multilDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("多选");
- // 多选的元素
- final String[] items = { "呵呵", "哈哈", "嘻嘻", "dj", "ds", "sd" };
- // 选中的元素,与多选的元素对应,被选中则为true
- final boolean[] checkedItems = new boolean[items.length];
- // 设置多选的元素
- builder.setMultiChoiceItems(items, checkedItems,
- new OnMultiChoiceClickListener() {
-
- /**
- * @parm which-->选中的元素
- * @parm isChecked-->该元素是否被选中
- */
- @Override
- public void onClick(DialogInterface dialog, int which,
- boolean isChecked) {
- Toast.makeText(MainActivity.this,
- items[which] + "======" + isChecked,
- Toast.LENGTH_LONG).show();
- AlertDialog.Builder builders = new AlertDialog.Builder(
- MainActivity.this);
- builders.setNegativeButton("取消",
- new AlertDialog.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- Log.e("取消", "取消了选择");
- }
- });
- builders.setPositiveButton("确定",
- new AlertDialog.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog,
- int which) {
- }
- });
- builders.show();
- }
- });
- builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- for (int i = 0; i < checkedItems.length; i++) {
- if (checkedItems[i]) {
- Log.e(i + ":", items[i]);
- }
- }
- }
- });
- builder.show();
- }
-
- /**
- * 日期对话框
- */
- public void dateDialog() {
- final Calendar calendar = Calendar.getInstance();
- final DatePickerDialog datePickerDialog = new DatePickerDialog(this,
- new OnDateSetListener() {
- /**
- * @parm view 监听的对话框中显示的日期的改变
- * @parm year 对话框消失前所显示的年份
- * 。。。
- */
- @Override
- public void onDateSet(DatePicker view, int year,
- int monthOfYear, int dayOfMonth) {
- System.out.println("11111111111111");
- calendar.set(Calendar.YEAR, year);
- calendar.set(Calendar.MONTH, monthOfYear);
- calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
- updateDate(calendar);
- Toast.makeText(MainActivity.this, "hehe......",
- Toast.LENGTH_LONG).show();
- System.out.println("---------------");
- }
- }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
- calendar.get(Calendar.DAY_OF_MONTH));
- //设置按键,会先执行
- datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "消极", new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // datePickerDialog.show();
- }
- });
- datePickerDialog.show();
- }
-
- /**
- * 更新日期
- */
- public void updateDate(Calendar calendar) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("选择的日期");
- builder.setMessage(calendar.get(Calendar.YEAR) + "年"
- + (calendar.get(Calendar.MONTH) + 1) + "月"
- + calendar.get(Calendar.DAY_OF_MONTH) + "日");
- builder.show();
- }
-
- /**
- * 时间对话框
- */
- public void timeDialog() {
- final Calendar calendar = Calendar.getInstance();
- TimePickerDialog timePickerDialog = new TimePickerDialog(this,
- new OnTimeSetListener() {
-
- @Override
- public void onTimeSet(TimePicker view, int hourOfDay,
- int minute) {
- calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
- calendar.set(Calendar.MINUTE, minute);
- updateTime(calendar);
- }
- }, calendar.get(Calendar.HOUR_OF_DAY),
- calendar.get(Calendar.MINUTE), true/*24小时制*/);
- timePickerDialog.show();
- }
-
- /**
- * 更新日期
- */
- public void updateTime(Calendar calendar) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("选择的时间");
- builder.setMessage(calendar.get(Calendar.HOUR_OF_DAY) + "时"
- + calendar.get(Calendar.MINUTE) + "分");
- builder.show();
- }
-
- /**
- * 自定义对话框
- */
- public void myDialog() {
- MyDialog myDialog = new MyDialog(this);
- myDialog.show();
- }
-
- /**
- * 局部自定义,自定义ListView、按键、消息内容等
- */
- public void myAlertDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- //1用listView作为视图
- // ListView listView = new ListView(this);
- //2用布局过滤器设置视图
- LayoutInflater li = LayoutInflater.from(this);
- View view = li.inflate(R.layout.dialog, null);
- //3.设置Activity作为对话框
-
- //列表内容适配器设置
- //设置对话框的视图
- // builder.setView(listView);
- builder.setView(view);
-
- }
-
- /**
- * Activity作为对话框
- */
- public void dialogActivity() {
- Intent intent = new Intent(this, DialogActivity.class);
- startActivity(intent);
- }
-
-
- /**
- * ProgressDialog
- */
- public void progressDialog() {
- pDialog = new ProgressDialog(this);
- /*
- * 设置风格精确进度条:ProgressDialog.STYLE_HORIZONTAL
- * 模糊进度条:ProgressDialog.STYLE_SPINNER
- */
- // 精确进度条
- pDialog.setProgressStyle(1);
- // 设置最大值
- pDialog.setMax(50);
-
- // 设置二级进度
- // pDialog.setSecondaryProgress(30);
- // 设置进度图片
- // pDialog.setProgressDrawable(d)
- // 模糊进度条
- // pDialog.setProgressStyle(0);
- // 设置消息
- pDialog.setMessage("请稍后。。。");
- // 设置进度条的图片
- // pDialog.setIndeterminateDrawable(d)
- // 设置对话框的按钮
- pDialog.setButton(DialogInterface.BUTTON_NEUTRAL/* 按键形式 */, "确认",
- new DialogInterface.OnClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which) {
-
- }
- });
- pDialog.show();
- // 设置当前进度,要在show之后
- // pDialog.setProgress(20);
-
- // 进度条加载完就消失
- // if (pDialog.getProgress() == pDialog.getMax()) {
- // pDialog.dismiss();
- // }
- Thread thread = new Thread(this);
- thread.start();
- }
-
- @Override
- public void onClick(View v) {
- if (v.getId() == R.id.dailog_tv) {
- // messageDialog();
- // itemDialog();
- // singleDialog();
- // multilDialog();
- // progressDialog();
- // dateDialog();
- // timeDialog();
- dialogActivity();
- }
- }
-
- @Override
- public void run() {
- int temp = 0;
- while (!(pDialog.getProgress() == pDialog.getMax())) {
- temp += 10;
- try {
- Thread.sleep(1000);
- pDialog.setProgress(temp);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- pDialog.dismiss();
- }
-
- }
将Activity作为Dialog的形式显示,只需要将Activity的清单中设置属性为Dialog即可,如下
android:theme="@android:style/Theme.Dialog"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。