Android学习第十二天--AlertDialog+handler

主界面无需太大的修改,设置一些需要的属性,就行了。

<RelativeLayout 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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="40dp"
        android:text="Button" />

</RelativeLayout>

java代码中

package cn.will.test;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
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.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String[] info={"aaa","vbv","ddd"};
    
    Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            
            if(msg.what==0)
            {
                Toast.makeText(MainActivity.this, "更新已完成", 2000).show();
            }
        }
    };
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button mButton=(Button)findViewById(R.id.button1);
        mButton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                AlertDialog.Builder mBuilder=new Builder(MainActivity.this);
                mBuilder.setTitle("alkjsdh");
                mBuilder.setItems(info, new OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface arg0, int which) {
                        if(which==0)
                        {
                            final ProgressDialog pd=new ProgressDialog(MainActivity.this);
                            pd.setTitle("稍等");
                            pd.setMessage("请稍等一会~~~");
                            
                            pd.show();
                            new Thread(new Runnable() {
                                
                                @Override
                                public void run() {
                                    try {
                                        Thread.sleep(10000);
                                        pd.dismiss();
                                        
                                        Message msg=new Message();
                                        msg.what=0;
                                        mHandler.sendMessage(msg);
                                        
                                        
                                    } catch (InterruptedException e) {
                                        
                                        e.printStackTrace();
                                    }
                                    
                                    
                                    
                                }
                            }).start();
                        }
                    }
                });
                mBuilder.create();
                mBuilder.show();
            }
        });
    }
}

 

posted @ 2013-03-20 00:58  小三小山  阅读(440)  评论(0编辑  收藏  举报