安卓开发:四种对话框
Posted xuyiqing
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓开发:四种对话框相关的知识,希望对你有一定的参考价值。
先展示一下四种对话框的效果图:
普通对话框
单选对话框:
多选对话框:
进度条对话框:
一个简单的布局,放四个按钮:
<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" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click1" android:text="普通对话框" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click2" android:text="单选对话框" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click3" android:text="多选对话框" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click4" android:text="进度条对话框" /> </LinearLayout>
四种对话框:
package org.dreamtech.dialog; import android.os.Bundle; import android.os.SystemClock; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // 普通对话框 public void click1(View v) { AlertDialog.Builder builder = new Builder(this); builder.setTitle("对话框标题"); builder.setMessage("对话框内容"); builder.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "点击了确定按钮", Toast.LENGTH_LONG).show(); } }); builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "点击了取消按钮", Toast.LENGTH_LONG).show(); } }); builder.show(); } // 单选对话框 public void click2(View v) { AlertDialog.Builder builder = new Builder(this); builder.setTitle("对话框标题"); final String items[] = { "选项1", "选项2", "选项3" }; builder.setSingleChoiceItems(items, -1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String item = items[which]; Toast.makeText(getApplicationContext(), "选中" + item, Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); builder.show(); } // 多选对话框 public void click3(View v) { AlertDialog.Builder builder = new Builder(this); builder.setTitle("对话框标题"); final String items[] = { "选项1", "选项2", "选项3" }; final boolean checkItems[] = { false, false, false }; builder.setMultiChoiceItems(items, checkItems, new OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { } }); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < checkItems.length; i++) { if (checkItems[i]) { String choice = items[i]; sb.append(choice + " "); } } Toast.makeText(getApplicationContext(), "选中" + sb.toString(), Toast.LENGTH_LONG).show(); dialog.dismiss(); } }); builder.show(); } // 进度条对话框 public void click4(View v) { final ProgressDialog dialog = new ProgressDialog(this); dialog.setTitle("正在加载"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.show(); new Thread() { public void run() { dialog.setMax(100); for (int i = 0; i <= 100; i++) { dialog.setProgress(i); SystemClock.sleep(50); } dialog.dismiss(); }; }.start(); } }
以上是关于安卓开发:四种对话框的主要内容,如果未能解决你的问题,请参考以下文章
安卓开发重写onBackPressed()方法不能处理对话框返回事件