Android Dialog的使用例子Demo
第一个XML,布局的
View Code
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 11 <Button 12 android:id="@+id/MyButtonClick" 13 android:text="普通按钮01" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 /> 17 <Button 18 android:id="@+id/MyButtonClick02" 19 android:text="文本按钮02" 20 android:layout_below="@id/MyButtonClick" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 /> 24 <Button 25 android:id="@+id/MyButtonClick03" 26 android:text="普通按钮03" 27 android:layout_below="@id/MyButtonClick02" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 /> 31 <Button 32 android:id="@+id/MyButtonClick04" 33 android:text="普通按钮04" 34 android:layout_below="@id/MyButtonClick03" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 /> 38 </RelativeLayout>
第二个XML,实现第三个按钮的
View Code
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="fill_parent" 6 android:layout_height="fill_parent"> 7 8 <TextView 9 android:id="@+id/username_view" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:layout_marginLeft="20dip" 13 android:layout_marginRight="20dip" 14 android:text="用户名" 15 android:textAppearance="@android:style/TextAppearance.Medium" 16 /> 17 <EditText 18 android:id="@+id/username_edit" 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:layout_marginLeft="20dip" 22 android:layout_marginRight="20dip" 23 android:capitalize="none" 24 android:textAppearance="@android:style/TextAppearance.Medium" 25 /> 26 <TextView 27 android:id="@+id/password_view" 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:layout_marginLeft="20dip" 31 android:layout_marginRight="20dip" 32 android:text="密码" 33 android:textAppearance="@android:style/TextAppearance.Medium" 34 /> 35 <EditText 36 android:id="@+id/password_edit" 37 android:layout_width="fill_parent" 38 android:layout_height="wrap_content" 39 android:layout_marginLeft="20dip" 40 android:layout_marginRight="20dip" 41 android:capitalize="none" 42 android:password="true" 43 android:textAppearance="@android:style/TextAppearance.Medium" 44 /> 45 </LinearLayout>
Activity
View Code
1 package com.example.clicktalk; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.AlertDialog; 6 import android.app.AlertDialog.Builder; 7 import android.app.Dialog; 8 import android.app.ProgressDialog; 9 import android.content.Context; 10 import android.content.DialogInterface; 11 import android.view.LayoutInflater; 12 import android.view.Menu; 13 import android.view.View; 14 import android.widget.Button; 15 import android.widget.Toast; 16 import android.widget.ToggleButton; 17 18 public class MainActivity extends Activity { 19 //按钮1 20 private static final int dialog1 = 1; 21 private Button button01; 22 //按钮2 23 private static final int dialog2 = 2; 24 private Button button02; 25 //按钮3 26 private static final int dialog3 = 3; 27 private Button button03; 28 //按钮4 29 private static final int dialog4 = 4; 30 private Button button04; 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 //绑定监听按钮1 36 button01 = (Button)findViewById(R.id.MyButtonClick); 37 button01.setOnClickListener(new Button.OnClickListener(){ 38 @Override 39 public void onClick(View v) { 40 showDialog(dialog1); 41 // TODO Auto-generated method stub 42 //Toast.makeText(MainActivity.this, "iu", Toast.LENGTH_LONG).show(); 43 } 44 45 }); 46 //绑定监听按钮2 47 button02 = (Button)findViewById(R.id.MyButtonClick02); 48 button02.setOnClickListener(new Button.OnClickListener(){ 49 @Override 50 public void onClick(View v) { 51 // TODO Auto-generated method stub 52 showDialog(dialog2); 53 } 54 }); 55 //绑定监听按钮3 56 button03 = (Button)findViewById(R.id.MyButtonClick03); 57 button03.setOnClickListener(new Button.OnClickListener(){ 58 59 @Override 60 public void onClick(View v) { 61 // TODO Auto-generated method stub 62 showDialog(dialog3); 63 } 64 }); 65 //绑定监听按钮4 66 button04 = (Button)findViewById(R.id.MyButtonClick04); 67 button04.setOnClickListener(new Button.OnClickListener(){ 68 69 @Override 70 public void onClick(View v) { 71 // TODO Auto-generated method stub 72 showDialog(dialog4); 73 } 74 }); 75 } 76 77 protected Dialog onCreateDialog(int id) { 78 switch (id) { 79 case dialog1: 80 return buildDialog1(MainActivity.this); 81 case dialog2: 82 return buildDialog2(MainActivity.this); 83 case dialog3: 84 return buildDialog3(MainActivity.this); 85 case dialog4: 86 return buildDialog4(MainActivity.this); 87 } 88 return null; 89 } 90 91 private Dialog buildDialog1(Context context) { 92 AlertDialog.Builder builder = new AlertDialog.Builder(context); 93 builder.setTitle("who are you?"); 94 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 95 96 @Override 97 public void onClick(DialogInterface dialog, int which) { 98 // TODO Auto-generated method stub 99 Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show(); 100 } 101 }); 102 builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 103 104 @Override 105 public void onClick(DialogInterface dialog, int which) { 106 // TODO Auto-generated method stub 107 Toast.makeText(MainActivity.this, "取消按钮", Toast.LENGTH_SHORT).show();//在第一个参数中用this显示makeText红线,用MainActivity.this表本身即可 108 } 109 }); 110 return builder.create(); 111 } 112 private Dialog buildDialog2(Context context) { 113 AlertDialog.Builder builder = new AlertDialog.Builder(context); 114 builder.setTitle("button02"); 115 builder.setMessage("who are you?"); 116 builder.setPositiveButton("Yes", null); 117 builder.setNegativeButton("No", null); 118 return builder.create(); 119 } 120 private Dialog buildDialog3(Context context) { 121 LayoutInflater inflater = LayoutInflater.from(this); 122 final View textEntryView = inflater.inflate(R.layout.alert_dialog_text_entry, null); 123 124 AlertDialog.Builder builder = new AlertDialog.Builder(context); 125 builder.setTitle("Hello"); 126 builder.setView(textEntryView);//关键 127 builder.setPositiveButton("YES", null); 128 builder.setNegativeButton("NO", null); 129 return builder.create(); 130 } 131 private Dialog buildDialog4(Context context) { 132 ProgressDialog dialog = new ProgressDialog(context); 133 dialog.setTitle("waiting"); 134 dialog.setMessage("loading"); 135 return dialog; 136 } 137 public boolean onCreateOptionsMenu(Menu menu) { 138 // Inflate the menu; this adds items to the action bar if it is present. 139 getMenuInflater().inflate(R.menu.main, menu); 140 return true; 141 } 142 143 }
参照http://www.cnblogs.com/hailiang/archive/2011/07/14/2106392.html
记下代码,方便以后查看