本文出自:http://blog.csdn.net/wanzdhan/article/details/7024834
文字就不说了,直接上代码。
1.三个选项Dialog.
效果图如下:
- /**
- * 三个选项的Dialog
- */
- private void threeDialog(){
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("带进度条的Dialog");
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNeutralButton("中间", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "中间", Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();
- }
- });
- builder.create().show();
- }
二、进度条Dialog。
代码:
- /**
- * 进度条Dialog
- */
- private void progressDialog(){
- mProgress = new ProgressDialog(mContext);
- mProgress.setIcon(R.drawable.ic_launcher);
- mProgress.setTitle("带进度条的Dialog");
- mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- mProgress.setButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();
- }
- });
- mProgress.setButton2("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();
- }
- });
- mProgress.show();
- new Thread(new Runnable() {
- int progress = 0;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (progress <= 100) {
- mProgress.setProgress(progress);
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- progress++;
- }
- }
- }).start();
- }
三、单选Dialog
- /**
- * 单选Dialog
- */
- private void simpleDialog(){
- singleSelectedId = -1;
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("单选Dialog");
- builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- singleSelectedId = which;
- Toast.makeText(mContext, "你选择的ID为:"+which, Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- if (singleSelectedId >= 0) {
- Toast.makeText(mContext, "你选择的ID为:"+singleSelectedId, Toast.LENGTH_SHORT).show();
- } else {
- singleSelectedId = 0;
- // 业务逻辑
- }
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- builder.create().show();
- }
四、多选复选Dialog
- /**
- * 复选Dialog
- */
- private void chooseDialog(){
- boolean[] selected = new boolean[items.length];
- for (int i = 0; i < selected.length; i++) {
- selected[i] = false;
- }
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("复选Dialog");
- builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- // TODO Auto-generated method stub
- if (isChecked) {
- list.add(which);
- } else {
- list.remove(which);
- }
- }
- });
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- String str = "你选择了:";
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- for (int i = 0; i < list.size(); i++) {
- str += " "+items[list.get(i)];
- }
- Toast.makeText(mContext, str, Toast.LENGTH_LONG).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- builder.create().show();
- }
五、自定义Dialog
- /**
- * 自定义Dialog
- */
- EditText username;
- EditText password;
- private void customDialog(){
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- LayoutInflater flater = LayoutInflater.from(mContext);
- final View view = flater.inflate(R.layout.login, null);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("自定义Dialog");
- builder.setView(view);
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- username = (EditText) view.findViewById(R.id.username);
- password = (EditText) view.findViewById(R.id.pwd);
- Toast.makeText(mContext, ""+username.getText()+password.getText(), Toast.LENGTH_LONG).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- builder.create().show();
- }
整体代码如下:
HelloDialogActivity.java:
- package com.covics.zfh;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.text.style.BulletSpan;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class HelloDialogActivity extends Activity implements OnClickListener {
- private Button btnThree;
- private Button btnProgress;
- private Button btnSingle;
- private Button btnChoices;
- private Button btnCustom;
- private Context mContext;
- private ProgressDialog mProgress;
- private int singleSelectedId; // 单选ID
- private String[] items = {"item0", "item1", "item2", "item3", "item4", "item5"};
- private ArrayList<Integer> list;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mContext = HelloDialogActivity.this;
- prepare();
- }
- private void prepare(){
- btnThree = (Button)findViewById(R.id.btn_three);
- btnProgress = (Button)findViewById(R.id.btn_progress);
- btnSingle = (Button)findViewById(R.id.btn_dan);
- btnChoices = (Button)findViewById(R.id.btn_mus);
- btnCustom = (Button)findViewById(R.id.btn_custom);
- btnThree.setOnClickListener(this);
- btnProgress.setOnClickListener(this);
- btnSingle.setOnClickListener(this);
- btnChoices.setOnClickListener(this);
- btnCustom.setOnClickListener(this);
- }
- /**
- * 三个选项的Dialog
- */
- private void threeDialog(){
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("带进度条的Dialog");
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNeutralButton("中间", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "中间", Toast.LENGTH_SHORT).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();
- }
- });
- builder.create().show();
- }
- /**
- * 进度条Dialog
- */
- private void progressDialog(){
- mProgress = new ProgressDialog(mContext);
- mProgress.setIcon(R.drawable.ic_launcher);
- mProgress.setTitle("带进度条的Dialog");
- mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- mProgress.setButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "确定", Toast.LENGTH_SHORT).show();
- }
- });
- mProgress.setButton2("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Toast.makeText(mContext, "取消", Toast.LENGTH_SHORT).show();
- }
- });
- mProgress.show();
- new Thread(new Runnable() {
- int progress = 0;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (progress <= 100) {
- mProgress.setProgress(progress);
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- progress++;
- }
- }
- }).start();
- }
- /**
- * 单选Dialog
- */
- private void simpleDialog(){
- singleSelectedId = -1;
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("单选Dialog");
- builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- singleSelectedId = which;
- Toast.makeText(mContext, "你选择的ID为:"+which, Toast.LENGTH_SHORT).show();
- }
- });
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- if (singleSelectedId >= 0) {
- Toast.makeText(mContext, "你选择的ID为:"+singleSelectedId, Toast.LENGTH_SHORT).show();
- } else {
- singleSelectedId = 0;
- // 业务逻辑
- }
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- builder.create().show();
- }
- /**
- * 复选Dialog
- */
- private void chooseDialog(){
- boolean[] selected = new boolean[items.length];
- for (int i = 0; i < selected.length; i++) {
- selected[i] = false;
- }
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("复选Dialog");
- builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- // TODO Auto-generated method stub
- if (isChecked) {
- list.add(which);
- } else {
- list.remove(which);
- }
- }
- });
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- String str = "你选择了:";
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- for (int i = 0; i < list.size(); i++) {
- str += " "+items[list.get(i)];
- }
- Toast.makeText(mContext, str, Toast.LENGTH_LONG).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- builder.create().show();
- }
- /**
- * 自定义Dialog
- */
- EditText username;
- EditText password;
- private void customDialog(){
- AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
- LayoutInflater flater = LayoutInflater.from(mContext);
- final View view = flater.inflate(R.layout.login, null);
- builder.setIcon(R.drawable.ic_launcher);
- builder.setTitle("自定义Dialog");
- builder.setView(view);
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- username = (EditText) view.findViewById(R.id.username);
- password = (EditText) view.findViewById(R.id.pwd);
- Toast.makeText(mContext, ""+username.getText()+password.getText(), Toast.LENGTH_LONG).show();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- }
- });
- builder.create().show();
- }
- @Override
- public void onClick(View v) {
- int id = v.getId();
- switch (id) {
- case R.id.btn_three : {
- threeDialog();
- break;
- }
- case R.id.btn_progress : {
- progressDialog();
- break;
- }
- case R.id.btn_dan: {
- simpleDialog();
- break;
- }
- case R.id.btn_mus : {
- list = new ArrayList<Integer>();
- chooseDialog();
- break;
- }
- case R.id.btn_custom : {
- customDialog();
- break;
- }
- default :
- break;
- }
- }
- }
layout中文件:
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Dialog集合" />
- <Button
- android:id="@+id/btn_three"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="三个选项的Dialog"
- />
- <Button
- android:id="@+id/btn_progress"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="进度条Dialog"
- />
- <Button
- android:id="@+id/btn_dan"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="单选Dialog"
- />
- <Button
- android:id="@+id/btn_mus"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="多选Dialog"
- />
- <Button
- android:id="@+id/btn_custom"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="自定义Dialog"
- />
- </LinearLayout>
login.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:id="@+id/user_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <TextView
- android:id="@+id/text_user"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="姓名:"
- />
- <EditText
- android:id="@+id/username"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:saveEnabled="true"
- android:hint="姓名"
- />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/pwd_layout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <TextView
- android:id="@+id/text_pwd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="密码:"
- />
- <EditText
- android:id="@+id/pwd"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:inputType="textPassword"
- android:saveEnabled="true"
- android:hint="密码"
- />
- </LinearLayout>
- </LinearLayout>