第一个XML,布局的
View Code
110 11 17 24 31 38
第二个XML,实现第三个按钮的
View Code
1 27 8 17 26 35 45
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 @Override105 public void onClick(DialogInterface dialog, int which) {106 // TODO Auto-generated method stub107 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 }
参照
记下代码,方便以后查看