android Dialog對話框使用示例

ch7_dialog.xml:html

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="這是一個對話框的示例"/>
    <Button android:id="@+id/bt_showCommonDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示普通對話框"/>
    
    <Button android:id="@+id/bt_showButtonDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示帶按鈕的對話框"/>
    
    <Button android:id="@+id/bt_showInputDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示輸入對話框"/>
    
    <Button android:id="@+id/bt_showListDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示列表對話框"/>
    
    <Button android:id="@+id/bt_showRadioDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示單選按鈕對話框"/>
    
    <Button android:id="@+id/bt_showCheckBoxDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示覆選框對話框"/>
    
    <Button android:id="@+id/bt_showDatetimePcikDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示日期時間對話框"/>
    
    <Button android:id="@+id/bt_showProgressDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示進度條對話框"/>
    
    <Button android:id="@+id/bt_showMyDialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="顯示自定義對話框"/>
    
    

</LinearLayout>

ch7_login.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="用戶名: "/>
        <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        
    </LinearLayout>
    
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="密  碼: "/>
        <EditText android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        
    </LinearLayout>

</LinearLayout>

DialogActivity.java:

package com.example.ch7;

import java.util.Calendar;

import com.example.baseexample.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class DialogActivity extends Activity{
	
	private Button bt_showCommonDialog;
	private Button bt_showButtonDialog;
	private Button bt_showInputDialog;
	private Button bt_showListDialog;
	private Button bt_showRadioDialog;
	private Button bt_showCheckBoxDialog;
	private Button bt_showDatetimePcikDialog;
	private Button bt_showProgressDialog;
	private Button bt_showMyDialog;
	
	final String[] arrayHobby = {"籃球","足球","羽毛球","乒乓球"};
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch7_dialog);
		bt_showCommonDialog = (Button)findViewById(R.id.bt_showCommonDialog);
		bt_showButtonDialog = (Button)findViewById(R.id.bt_showButtonDialog);
		bt_showInputDialog = (Button)findViewById(R.id.bt_showInputDialog);
		bt_showListDialog = (Button)findViewById(R.id.bt_showListDialog);
		bt_showRadioDialog = (Button)findViewById(R.id.bt_showRadioDialog);
		bt_showCheckBoxDialog = (Button)findViewById(R.id.bt_showCheckBoxDialog);
		bt_showDatetimePcikDialog = (Button)findViewById(R.id.bt_showDatetimePcikDialog);
		bt_showProgressDialog = (Button)findViewById(R.id.bt_showProgressDialog);
		bt_showMyDialog = (Button)findViewById(R.id.bt_showMyDialog);
		
		bt_showCommonDialog.setOnClickListener(new BtClickListener());
		bt_showButtonDialog.setOnClickListener(new BtClickListener());
		bt_showInputDialog.setOnClickListener(new BtClickListener());
		bt_showListDialog.setOnClickListener(new BtClickListener());
		bt_showRadioDialog.setOnClickListener(new BtClickListener());
		bt_showCheckBoxDialog.setOnClickListener(new BtClickListener());
		bt_showDatetimePcikDialog.setOnClickListener(new BtClickListener());
		bt_showProgressDialog.setOnClickListener(new BtClickListener());
		bt_showMyDialog.setOnClickListener(new BtClickListener());
		
	}
	
	class BtClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.bt_showCommonDialog:showDialog(1);break;
			case R.id.bt_showButtonDialog:showDialog(2);break;
			case R.id.bt_showInputDialog:showDialog(3);break;
			case R.id.bt_showListDialog:showDialog(4);break;
			case R.id.bt_showRadioDialog:showDialog(5);break;
			case R.id.bt_showCheckBoxDialog:showDialog(6);break;
			case R.id.bt_showDatetimePcikDialog:showDialog(7);break;
			case R.id.bt_showProgressDialog:showDialog(8);break;
			case R.id.bt_showMyDialog:showDialog(9);break;
			}
			
		}
		
	}
	
	protected Dialog onCreateDialog(int id){
		Dialog alertDialog = null;
		switch(id){
		case 1:
			alertDialog = new AlertDialog.Builder(this).setTitle("普通對話框").setMessage("這是一個普通對話框").setIcon(R.drawable.ic_launcher).create();
			break;
		case 2:
			alertDialog = new AlertDialog.Builder(this).setTitle("肯定退出?").setMessage("您肯定退出程序?").setIcon(R.drawable.ic_launcher).setPositiveButton("肯定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
					
				}
			}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			}).create();
			break;
		case 3:
			alertDialog = new AlertDialog.Builder(this).setTitle("輸入").setIcon(R.drawable.ic_launcher).setView(new EditText(this)).setPositiveButton("肯定", null).setNegativeButton("取消", null).create();
			break;
		case 4:
			alertDialog = new AlertDialog.Builder(this).setTitle("運動列表").setIcon(R.drawable.ic_launcher).setItems(arrayHobby, null).setPositiveButton("肯定", null).setNegativeButton("取消", null).create();
			break;
		case 5:
			alertDialog = new AlertDialog.Builder(this).setTitle("你喜歡哪一種運動").setIcon(R.drawable.ic_launcher).setSingleChoiceItems(arrayHobby, 0, null).setPositiveButton("確認", null).setNegativeButton("取消", null).create();
			break;
		case 6:
			alertDialog = new AlertDialog.Builder(this).setTitle("你喜歡哪些運動").setIcon(R.drawable.ic_launcher).setMultiChoiceItems(arrayHobby, null, null).setPositiveButton("確認", null).setNegativeButton("取消", null).create();
			break;
		case 7:
			Calendar c = Calendar.getInstance();
			alertDialog = new DatePickerDialog(this,null,c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
			break;
		case 8:
			ProgressDialog pd = new ProgressDialog(this);
			pd.setTitle("下載進度");
			pd.setMax(100);
			pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			pd.setProgress(10);
			pd.setCancelable(true);
			alertDialog = (Dialog)pd;
			break;
		case 9:
			LayoutInflater layoutInflater = LayoutInflater.from(this);
			View loginView = layoutInflater.inflate(R.layout.ch7_login, null);
			alertDialog = new AlertDialog.Builder(this).setTitle("用戶登陸").setIcon(R.drawable.ic_launcher).setView(loginView).setPositiveButton("登陸", null).setNegativeButton("取消", null).create();
			break;
		}
		return alertDialog;
	}

}
相關文章
相關標籤/搜索