Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 各種常用,不易記的dialog,等控件整理

android 各種常用,不易記的dialog,等控件整理

編輯:關於Android編程


[java] 
package com.su.testalldialog; 
 
import java.util.ArrayList; 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.Spinner; 
import android.widget.Toast; 
 
public class TestAllDialogActivity extends Activity { 
    String[] weekdays; 
    Context context; 
 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        context = this; 
        weekdays = new String[] { "Sun", "Mon", "Tue", "Wed" }; 
        Button button1 = (Button) findViewById(R.id.button1); 
        Button button2 = (Button) findViewById(R.id.button2); 
        Button button3 = (Button) findViewById(R.id.button3); 
        Button button4 = (Button) findViewById(R.id.button4); 
        Button button5 = (Button) findViewById(R.id.button5); 
        Button button6 = (Button) findViewById(R.id.button6); 
        button6.setOnClickListener(listener); 
        button5.setOnClickListener(listener); 
        button1.setOnClickListener(listener); 
        button2.setOnClickListener(listener); 
        button3.setOnClickListener(listener); 
        button4.setOnClickListener(listener); 
        initSpinner(); 
        initAutocompletetextview(); 
 
    } 
 
    private void initAutocompletetextview() { 
        AutoCompleteTextView autocompletetextview = (AutoCompleteTextView) findViewById(R.id.autocompletetextview); 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, 
                android.R.layout.simple_spinner_dropdown_item, weekdays);// 實現簡單的adapter 
        autocompletetextview.setAdapter(adapter); 
    } 
 
    private void initSpinner() { 
        Spinner spinner = (Spinner) findViewById(R.id.spinner); 
        spinner.setOnItemSelectedListener(slistener); 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, 
                android.R.layout.simple_spinner_item, weekdays);// 實現簡單的adapter 
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 實現選項的樣式 
        spinner.setAdapter(adapter); 
    } 
 
    private OnItemSelectedListener slistener = new OnItemSelectedListener() { 
 
        @Override 
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
                long arg3) { 
            toast(weekdays[arg2]); 
 
        } 
 
        @Override 
        public void onNothingSelected(AdapterView<?> arg0) { 
            // TODO Auto-generated method stub 
 
        } 
    }; 
 
    int index = 0;// 用來標記哪一項被選擇 
    private OnClickListener listener = new OnClickListener() { 
 
        @Override 
        public void onClick(View v) { 
 
            Builder dialog = new AlertDialog.Builder(context); 
            switch (v.getId()) { 
 
            case R.id.button1: 
                dialog.setIcon(R.drawable.ic_launcher); 
                dialog.setTitle("簡單選擇對話框"); 
                dialog.setItems(weekdays, 
                        new DialogInterface.OnClickListener() { 
 
                            @Override 
                            public void onClick(DialogInterface dialog, 
                                    int which) { 
                                toast(weekdays[which]); 
 
                            } 
                        }); 
                dialog.show(); 
                break; 
            case R.id.button2: 
                dialog.setIcon(R.drawable.ic_launcher); 
                dialog.setTitle("單選對話框"); 
                dialog.setSingleChoiceItems(weekdays, 1, 
                        new DialogInterface.OnClickListener() { 
 
                            public void onClick(DialogInterface dialog, 
                                    int which) { 
                                index = which; 
 
                            } 
                        }); 
                dialog.setPositiveButton("是", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
                                toast(weekdays[index]); 
                            } 
                        }); 
                dialog.setNegativeButton("否", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
 
                            } 
                        }); 
 
                dialog.show(); 
                break; 
            case R.id.button3: 
                final ArrayList<String> multiWeekadys = new ArrayList<String>();// 選擇的數據放到list中 
 
                dialog.setIcon(R.drawable.ic_launcher); 
                dialog.setTitle("復選對話框"); 
                final boolean[] checkeditems = new boolean[weekdays.length];// 設置一打開時的選擇情況 
                dialog.setMultiChoiceItems(weekdays, checkeditems, 
                        new DialogInterface.OnMultiChoiceClickListener() { 
 
                            @Override 
                            public void onClick(DialogInterface dialog, 
                                    int which, boolean isChecked) { 
                                if (isChecked) { 
                                    checkeditems[which] = true;// 獲取一個被選中的列表 
 
                                } 
 
                            } 
                        }); 
 
                dialog.setPositiveButton("是", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
                                for (int j = 0; j < checkeditems.length; j++) {// 遍歷一遍看那個是選中的 
                                    if (checkeditems[j]) { 
                                        multiWeekadys.add(weekdays[j]); 
                                    } 
 
                                } 
 
                                toast(multiWeekadys.toString()); 
                            } 
                        }); 
                dialog.setNegativeButton("否", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
 
                            } 
                        }); 
                dialog.show(); 
                break; 
            case R.id.button4: 
                dialog.setIcon(R.drawable.ic_launcher); 
                dialog.setTitle("簡單警告"); 
                dialog.setMessage("測試警告"); 
                dialog.setPositiveButton("是", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
                                toast("是"); 
 
                            } 
                        }); 
                dialog.setNeutralButton("中", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
                                toast("中"); 
                            } 
                        }); 
                dialog.setNegativeButton("否", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
 
                            } 
                        }); 
 
                dialog.show(); 
                break; 
            case R.id.button5: 
                final EditText et = new EditText(context); 
                et.setText("測試"); 
                dialog.setIcon(R.drawable.ic_launcher); 
                dialog.setTitle("簡單警告"); 
                dialog.setMessage("測試警告"); 
                dialog.setView(et); 
                dialog.setPositiveButton("是", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
                                toast(et.getText().toString()); 
 
                            } 
                        }); 
                dialog.setNeutralButton("中", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
                                toast("中"); 
                            } 
                        }); 
                dialog.setNegativeButton("否", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
 
                            } 
                        }); 
 
                dialog.show(); 
                break; 
            case R.id.button6: 
 
                final ImageView im = new ImageView(context); 
                im.setImageResource(R.drawable.ic_launcher); 
 
                dialog.setIcon(R.drawable.ic_launcher); 
                dialog.setTitle("簡單警告"); 
                dialog.setMessage("測試警告"); 
                dialog.setView(im); 
 
                dialog.setPositiveButton("是", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
 
                            } 
                        }); 
                dialog.setNeutralButton("中", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
                                toast("中"); 
                            } 
                        }); 
                dialog.setNegativeButton("否", 
                        new DialogInterface.OnClickListener() { 
                            public void onClick(DialogInterface dialog, int i) { 
 
                            } 
                        }); 
 
                dialog.show(); 
                break; 
 
            default: 
                break; 
            } 
 
        } 
    }; 
 
    private void toast(String msg) { 
        Toast.makeText(context, msg, 10).show(); 
 
    } 
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved