创建dialog需要用到AlertDialog.Builder类,这可以理解为一个对话框的产生器,可以利用这个类创建各种种类的对话框。对话框创建好之后调用此类的create方法即可产生一个对话框,再调用show方法就可以将对话框显示到界面上。
如果对话框用来展示一个进度,可以使用ProgressDialog类来完成(不建议使用,应该用ProgressBar代替)。另外还有两类对话框一个叫DatePickerDialog另一个是TimePickerDialog分别用来选择日期和时间。
除此之外,对话框可以使用DialogFrgament类来包装,但是为了兼容性需要导入v4的包。具体可以看文档。
下面以代码片段的方式总结了一些常用的dialog的使用:
1.最基本的dialog:
显示效果:
代码片段:
public void show_alert() { AlertDialog.Builder builder = new Builder(this); builder.setMessage("是否更新"); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("提醒"); builder.setPositiveButton("确定",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了确定按钮",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show(); // dialog.dismiss(); } }); builder.show(); }
2.带一个列表的对话框:
(1).列表数据静态指定(放到数组中):
显示效果:
代码片段:
private void show_list() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("职业"); builder.setItems(new String[]{"程序员","工程师","砖家"},new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了"+which,Toast.LENGTH_SHORT).show(); } }); builder.show(); }
( 2).列表数据通过适配器指定
显示效果:
代码片段:
public void show_dialog() { /* 这里的数据可以从数据库/网络中动态获取 */ List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("image", R.drawable.image1); map1.put("text", "我是条目1"); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("image", R.drawable.image2); map2.put("text", "我是条目2"); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("image", R.drawable.image3); map3.put("text", "我是条目3"); data.add(map1); data.add(map2); data.add(map3); SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_item, new String[] { "image", "text" }, new int[] { R.id.iv_show, R.id.tv_text }); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("从适配器中获取的数据"); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "点击了" + which, 0).show(); } }); builder.show(); }
3.单选列表对话框
显示效果:
代码片段:
public void show_single() { AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("提醒"); builder.setSingleChoiceItems(new String[]{"条目1","条目2","条目3"},0,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which)//id从0开始 { Toast.makeText(MainActivity.this,"item="+which,Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show(); } }); builder.show(); }
4.多选列表对话框:
显示效果:
代码片段:
public void show_multiple() { AlertDialog.Builder builder = new Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("提醒"); builder.setMultiChoiceItems(new String[]{"条目1","条目2","条目3","条目4"}, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(MainActivity.this,"item"+which+","+isChecked,Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show(); } }); builder.show(); }
5.自定义对话框:
显示效果:
代码片段:
private void show_own_dialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater inflater = this.getLayoutInflater();//"充气筒" builder.setView(inflater.inflate(R.layout.dialog, null)) .setPositiveButton("登陆", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { Toast.makeText(MainActivity.this, "确定", 0).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Toast.makeText(MainActivity.this,"取消", 0).show(); } }); builder.show(); }
6.带进度条的dialog
显示效果:
代码片段:
public void show_circle_progressdialog() { ProgressDialog diaolog = new ProgressDialog(this); diaolog.setIcon(R.drawable.ic_launcher); diaolog.setTitle("提醒"); diaolog.setMessage("正在加载"); diaolog.show(); }
还有水平进度条:
显示效果:
代码片段:
public void show_horizonal() { final ProgressDialog diaolog = new ProgressDialog(this); diaolog.setIcon(R.drawable.ic_launcher); diaolog.setTitle("提醒"); diaolog.setMessage("正在加载"); diaolog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); diaolog.setMax(100); new Thread(new Runnable() { @Override public void run() { for(int i = 0; i<=100; i++) { diaolog.setProgress(i); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } diaolog.dismiss(); } }).start(); diaolog.show(); }
7.日期选择与时间选择对话框:
显示效果:
代码片段:
布局:
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <DatePicker android:id="@+id/dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/tp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>activity:
package com.example.dialogdemo4; import android.app.Activity; import android.os.Bundle; import android.widget.DatePicker; import android.widget.Toast; import android.widget.DatePicker.OnDateChangedListener; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class MainActivity extends Activity { private DatePicker dp = null; private TimePicker tp = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dp = (DatePicker) findViewById(R.id.dp); tp = (TimePicker) findViewById(R.id.tp); dp.init(2014, 1, 1, new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this,"monthOfYear"+monthOfYear+",dayOfMonth"+dayOfMonth, 0).show(); } }); tp.setIs24HourView(true); tp.setOnTimeChangedListener(new OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { Toast.makeText(MainActivity.this,"hourOfDay"+hourOfDay+",minute"+minute, 0).show(); } }); } }
--------------------------------------------------------完--------------------------------------------------------
转载请注明出处,谢谢!
------------------------------------------------------------------------------------------------------------------