天天看點

Activity多功能按鈕——基礎編

<a target="_blank" href="http://blog.51cto.com/attachment/201102/135809295.jpg"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201102/135833507.jpg"></a>

<a target="_blank" href="http://blog.51cto.com/attachment/201102/135752156.jpg"></a>

package com.smart; 

import android.app.Activity; 

import android.app.AlertDialog; 

import android.app.Dialog; 

import android.content.DialogInterface; 

import android.os.Bundle; 

import android.util.Log; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button; 

import android.widget.ListView; 

//繼承OnClickListener 

public class Main extends Activity implements OnClickListener { 

//固定的靜态變量, 

    private final int DELETE_FILE = 1; 

    private final int SIMPLE_LIST = 2; 

    private final int SINGLE_CHOICE_LIST = 3; 

    private final int MULTI_LIST = 4; 

    private ListView lv = null; 

    //定義一個字元串數組 

    private String[] provinces = new String[] { "中國", "泰國", "美國", "法國", 

            "新加坡", "英國" }; 

    //按鈕 

    private ButtonOnClick buttonOnClick = new ButtonOnClick(1); 

    @Override//事件點選 

    public void onClick(View v) { 

        //開關選擇 

        switch(v.getId()){ 

        case R.id.btnDeleteFile: 

            showDialog(DELETE_FILE); 

            break; 

        case R.id.btnSimpleList: 

            showDialog(SIMPLE_LIST); 

        case R.id.btnChoiceList: 

            showDialog(SINGLE_CHOICE_LIST); 

        case R.id.btnMultiChoiceList: 

            showDialog(MULTI_LIST); 

        case R.id.btnRemoveDialog: 

            removeDialog(DELETE_FILE); 

            removeDialog(SIMPLE_LIST); 

            removeDialog(SINGLE_CHOICE_LIST); 

            removeDialog(MULTI_LIST); 

        } 

    } 

    @Override 

    protected Dialog onCreateDialog(int id) { 

        Log.d("dialog",String.valueOf(id)); 

        //用來判斷選擇的目标。如果沒有選擇,并提示出來,選擇中了,也顯示結果。詳看代碼 

        switch(id){ 

        case DELETE_FILE: 

            return new AlertDialog.Builder(this).setIcon(R.drawable.question).setTitle("是否删除檔案").setPositiveButton("确定", new DialogInterface.OnClickListener() { 

                @Override 

                public void onClick(DialogInterface dialog, int which) { 

                    new AlertDialog.Builder(Main.this).setMessage("檔案已經被删除").create().show(); 

                } 

            }).setNegativeButton("取消", new DialogInterface.OnClickListener() { 

                    new AlertDialog.Builder(Main.this).setMessage("您已經選擇了取消,檔案未删除").create().show(); 

            }).create(); 

        case SIMPLE_LIST: 

        return new AlertDialog.Builder(this).setTitle("選擇國家").setItems(provinces, new DialogInterface.OnClickListener() { 

            @Override 

            public void onClick(DialogInterface dialog, int which) { 

                final AlertDialog ad=new AlertDialog.Builder(Main.this).setMessage("您已經選擇了"+which+":"+provinces[which]).show(); 

            android.os.Handler hander=new android.os.Handler(); 

            hander.postDelayed(new Runnable() { 

                //線程管理 

                public void run() { 

                    ad.dismiss(); 

            },  5*1000); 

            } 

        }).create(); 

        case SINGLE_CHOICE_LIST: 

            return new AlertDialog.Builder(this).setTitle("選擇國家").setSingleChoiceItems(provinces, 1, buttonOnClick).setPositiveButton("确定", buttonOnClick).setNegativeButton("取消", buttonOnClick).create(); 

        case    MULTI_LIST: 

            AlertDialog ad=new AlertDialog.Builder(this).setIcon(R.drawable.image).setTitle("選擇國家").setMultiChoiceItems(provinces, new boolean[]{false,true,false,true,false,false} , new DialogInterface.OnMultiChoiceClickListener() { 

                public void onClick(DialogInterface dialog, int which, boolean isChecked) { 

            }).setPositiveButton("确定", new DialogInterface.OnClickListener() { 

                    int count=lv.getCount(); 

                    String s="您選擇了:"; 

                    for (int i = 0; i &lt; provinces.length; i++) { 

                        if(lv.getCheckedItemPositions().get(i)){ 

                            s+=i+":"+lv.getAdapter().getItem(i)+"  "; 

                        } 

                    } 

                    if(lv.getCheckedItemPositions().size()&gt;0){ 

                        new AlertDialog.Builder(Main.this).setMessage(s).show(); 

                    }else{ 

                        new AlertDialog.Builder(Main.this).setMessage("您未選擇任何國家").show(); 

            }).setNegativeButton("取消", null).create(); 

            lv=ad.getListView(); 

            return ad; 

        return null; 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

        //得到選中的哪一個按鈕 

        Button btnDeletefile=(Button)findViewById(R.id.btnDeleteFile); 

        Button btnSimpleList=(Button)findViewById(R.id.btnChoiceList); 

        Button btnSimngleChoiceList=(Button)findViewById(R.id.btnSimpleList); 

        Button btnMultiChoiceList=(Button)findViewById(R.id.btnMultiChoiceList); 

        Button btnRemoveDialog=(Button)findViewById(R.id.btnRemoveDialog); 

        //綁定監聽器 

        btnDeletefile.setOnClickListener(this); 

        btnSimpleList.setOnClickListener(this); 

        btnSimngleChoiceList.setOnClickListener(this); 

        btnMultiChoiceList.setOnClickListener(this); 

        btnRemoveDialog.setOnClickListener(this); 

    //定義一個類,用來判斷選擇中的。 

    private class ButtonOnClick implements DialogInterface.OnClickListener { 

        private int index; 

        public ButtonOnClick(int index) { 

            this.index = index; 

        @Override 

        public void onClick(DialogInterface dialog, int whichButton) { 

                if(whichButton&gt;=0){ 

                    index=whichButton; 

                }else{ 

                    if(whichButton==DialogInterface.BUTTON_POSITIVE){ 

                        new AlertDialog.Builder(Main.this).setMessage("您已經選擇了:"+index+":"+provinces[index]).show(); 

                    }else if(whichButton==DialogInterface.BUTTON_NEGATIVE){ 

                        new AlertDialog.Builder(Main.this).setMessage("你未選擇,請選擇").show(); 

protected void onPrepareDialog(int id,Dialog dialog){ 

    super.onPrepareDialog(id, dialog); 

另外很多朋友對Android系統構架圖還不是很了解,我附上一張中文的圖給大家觀看

<a target="_blank" href="http://blog.51cto.com/attachment/201102/140742220.jpg"></a>

本文轉自 llb988 51CTO部落格,原文連結:http://blog.51cto.com/llb988/489522,如需轉載請自行聯系原作者

繼續閱讀