Android AlertDialog

 1 package com.example.myact4;
 2 
 3 import android.app.AlertDialog;
 4 import android.content.DialogInterface;
 5 import android.os.Bundle;
 6 import android.os.Handler;
 7 import android.os.Message;
 8 import android.support.v7.app.ActionBarActivity;
 9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 import android.widget.TextView;
13 import android.widget.Toast;
14 /**
15  * AlertDialog练习
16  * @author shaobn
17  *
18  */
19 public class MainActivity extends ActionBarActivity {
20 private Button button;
21 private TextView tView;
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         setContentView(R.layout.activity_main);
26         button = (Button) this.findViewById(R.id.button1);
27         tView = (TextView) this.findViewById(R.id.textView1);
28         final Handler handler = new Handler(){
29             @Override
30             public void handleMessage(Message msg) {
31                 // TODO Auto-generated method stub
32                 super.handleMessage(msg);
33                 int num = msg.arg1;
34                 MainActivity.this.tView.setText(String.valueOf(num));
35                 Toast.makeText(MainActivity.this, Thread.currentThread().getName(), Toast.LENGTH_LONG).show();
36             }
37         };
38         button.setOnClickListener(new OnClickListener() {
39             
40             @Override
41             public void onClick(View arg0) {
42                 // TODO Auto-generated method stub
43             AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
44             builder.setIcon(R.drawable.ic_launcher);
45             builder.setTitle("友情提示");
46             builder.setMessage("您确定要删除吗?");
47             builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
48                 
49                 @Override
50                 public void onClick(DialogInterface arg0, int arg1) {
51                     // TODO Auto-generated method stub
52                     arg0.cancel();
53                 }
54             });
55             builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
56                 
57                 @Override
58                 public void onClick(DialogInterface arg0, int arg1) {
59                     // TODO Auto-generated method stub
60                     Runnable runnable=new Runnable() {
61                             
62                             @Override
63                             public void run() {
64                                 // TODO Auto-generated method stub
65                                 Message message = new Message();
66                                 message.arg1 = 3;
67                                 handler.sendMessage(message);
68                                 
69                             }
70                         };
71                     new Thread(runnable).start();
72                 }
73             });
74             AlertDialog alertDialog = builder.create();
75             alertDialog.show();
76             }
77         });
78     
79     
80     }    
81 }

 

posted @ 2015-09-10 15:52  邻家小书童  阅读(191)  评论(0编辑  收藏  举报