Android 对话框 Dialog使用实例讲解
对话框 Dialog
什么是对话框
对话框是在当前的页面之上弹出的小窗口, 用于显示一些重要的提示信息, 提示用户的输入,确认信息,或显示某种状态.如 : 显示进度条对话框, 退出提示.
对话框的特点:
1, 当前界面弹出的小窗口.
2, 用户要与它进行交互, 可以接收用户输入的信息, 也可以反馈信息给用户.
常用对话框:
1, 普通对话框 AlertDialog
2, 进度条对话框 ProgressDialog
3, 日期对话框 DatePickerDialog
4, 时间对话框 TimePickerDialog
5, 自定义对话框
常用普通对话框 AlertDialog
1, 普通列表对话框 setItems -- onClickListener
2, 单选对话框 setSingleChoiceItems -- onClickListener
3, 多选对话框 setMultiChoiceItems --- > onMultiChoiceClickListener
4, 适配器对话框 setAdapter -- onClickListener
package com.qf.day11_dialog_01; import java.util.Calendar; 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.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TimePicker; import android.widget.Toast; public class MainActivity extends Activity { private EditText name; private EditText pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showAlertDialog(View v) { //1, 定义普通对话框的构建者 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //2, 构建对话框 builder.setIcon(R.drawable.img_23);//设置图标 builder.setTitle("提示");//设置标题 builder.setMessage("是否确认退出?");//设置内容 //设置取消按钮 /** * text 按钮显示的文字 * listener 按钮对应的监听器 */ builder.setNegativeButton("取消", null); //设置确定按钮 builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); //3, 显示对话框 builder.show(); } public void showProgressDialog(View v) { //1, 得到ProgressDialog的对象 ProgressDialog pDialog = new ProgressDialog(this); //2, 构建对话框 pDialog.setIcon(R.drawable.img_14); pDialog.setTitle("提示"); pDialog.setMessage("正在加载, 请稍后...."); pDialog.setCancelable(true);//是否可撤销 true 可以 false 不可以 //设置进度条对话框的样式 //ProgressDialog.STYLE_HORIZONTAL 水平的 //ProgressDialog.STYLE_SPINNER 转圈的 pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //3, 显示对话框 pDialog.show(); //pDialog.dismiss();//对话框消失 pDialog.setProgress(50);//设置进度值, 在显示对话后,才可以设置 } public void showDatePickerDialog(View v) { //获取系统的当前日期 Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int monthOfYear = calendar.get(Calendar.MONTH); int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); //1, 创建带有日期选择器的对话框 /** * context 上下文对象 * callBack onDateSetListener(日期选择器上的"确定"按钮监听器) * year 默认的年 * monthOfYear 默认的月份 * dayOfMonth 默认的日期 */ DatePickerDialog dPDialog = new DatePickerDialog(this,new OnDateSetListener() { /** * DatePicker view 日期选择器 */ @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //选择后的日期监听 Toast.makeText(MainActivity.this, year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); } }, year, monthOfYear, dayOfMonth); //2, 显示对话框 dPDialog.show(); } public void showTimePickerDialog(View v) { //1, 创建带有时间选择器的对话框 /** * context 上下文对象 * callBack onTimeSetListener * hourOfDay 默认的小时 * minute 默认的分钟数 * is24HourView 是否24小时制 */ TimePickerDialog tpDialog = new TimePickerDialog(this, new OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this, hourOfDay + " :" + minute, Toast.LENGTH_SHORT).show(); } }, 15, 17, true); //2, 显示对话框 tpDialog.show(); } //自定义对话框(在普通对话框基础上实现) public void showCustomDialog(View v) { //1, 得到普通对话框的构建者 AlertDialog.Builder builder = new AlertDialog.Builder(this); //2, 构建对话框 builder.setIcon(R.drawable.img_23); builder.setTitle("用户登陆"); //3, 为对话框设置自定义的视图 View view = getLayoutInflater().inflate(R.layout.login, null); name = (EditText) view.findViewById(R.id.name); pwd = (EditText) view.findViewById(R.id.pwd); builder.setView(view); //为对话框添加 按钮 builder.setNegativeButton("取消", null); builder.setPositiveButton("登陆", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String info = name.getText().toString()+", "+ pwd.getText().toString(); Toast.makeText(MainActivity.this, info, Toast.LENGTH_SHORT).show(); } }); //4, 显示对话框 builder.show(); } }
package com.qf.day11_dialog_02; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; private int position;//单选列表的 中间转接 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); } //普通列表对话框 public void showCommon(View v) { //1, 得到普通对话框的构建者 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("选择颜色"); //2, 设置列表 //getResources().getStringArray(id) builder.setItems(R.array.colors, new OnClickListener() { /** * DialogInterface dialog 对话框 * int which 当前选中的item 从0开始 */ @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: tv.setTextColor(Color.RED); break; case 1: tv.setTextColor(Color.GREEN); break; case 2: tv.setTextColor(Color.BLUE); break; } } }); //3, 显示对话框 builder.show(); } //单选列表 public void showSingle(View v) { //1, 得到普通对话框的构建者 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("设置文字大小"); //2, 设置单选列表 /** * items 选项资源的ID R.array.sizes * checkedItem 默认选中的item的下标, 如果没有默认选中, 那么设置为-1 * listener 监听器 */ builder.setSingleChoiceItems(R.array.sizes, -1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 把当前选中的Item的下标 赋值给全局的变量 position position = which; } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO 根据全局的position 判断该执行什么样的操作 switch (position) { case 0: tv.setTextSize(20); break; case 1: tv.setTextSize(30); break; case 2: tv.setTextSize(40); break; } } }); //3, 显示对话框 builder.show(); } //多选列表 public void showMulti(View v) { //1, 得到普通对话框的构建者 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("设置内容"); //2, 设置多选列表 /** * items 资源的ID * checkedItems 当前选中的Item * listener 监听器 */ //得到所有的数据 final String[] hobbys = getResources().getStringArray(R.array.hobbys); //选中的Item final boolean[] checkedItems = new boolean[hobbys.length]; builder.setMultiChoiceItems(R.array.hobbys, checkedItems, new OnMultiChoiceClickListener() { /** * DialogInterface dialog 当前对话框 * int which 当前选择的Item下标 * boolean isChecked 当前选择Item的状态 */ @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { //改变boolean[] 中状态 checkedItems[which] = isChecked; } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuilder sBuilder = new StringBuilder(); for(int i=0;i<checkedItems.length;i++) { if(checkedItems[i]) { sBuilder.append(hobbys[i]).append(" "); } } tv.setText(sBuilder.toString()); } }); //3, 显示对话框 builder.show(); } //适配器列表 public void showAdapter(View v) { //1, 得到普通对话框的构建者 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("选择设置"); //2, 设置适配器 String[] data = getResources().getStringArray(R.array.settings); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); builder.setAdapter(adapter, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(); switch (which) { case 0: intent.setAction("android.settings.WIFI_SETTINGS"); break; case 1: intent.setAction("android.settings.SOUND_SETTINGS"); break; case 2: intent.setAction("android.settings.SETTINGS"); break; } startActivity(intent); } }); //3, 显示对话框 //builder.show(); builder.create().show(); } }
上一篇:Android多线程下载示例详解
栏 目:Android代码
下一篇:Android PopupWindow实现右侧、左侧和底部弹出菜单
本文地址:http://www.codeinn.net/misctech/13783.html