Android中各類Dialog實例——交互


      今天打開技術博客發現自己的上一篇技術博客已經是去年11月份的了。但凡是生活中或是學習中的許多事情我們一直都有在做,只是並沒有一直在記錄。有時候腦袋真的很像存儲機制,即使腦容量有着科學研究的浩如宇宙,但大腦之所謂有“大”來修飾,終歸還是會有溢出的時候。所以對於學習生活中運行過的“程序”所殘留的內存,要麼釋放,要麼記錄加以保管,而技術博客正是這樣的存在。

     做技術的不空談,現在開始。。。

 

     在androidApp的開發中會遇到許多需要人機交互的組件,藉此獲取用戶所作出的選擇或是反饋。這樣的一些組件到底有多少種又該如何實例呢? 看下圖:

 

       

             而這些組件各自的效果 如下圖:

 

 

            

 

        

 

   進行插入圖片操作時很想吐槽該網站。。。。。

 

    具體代碼實現如下:

 

package com.example.dialogtest;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.Toast;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;

public class MainActivity extends Activity implements Runnable {//Runnable線程接口 下面寫進度條選擇框時模擬使用
	private Button btn_diaNormal; 
	private Button btn_diaMulti;
	private Button btn_diaList;
	private Button btn_diaSinChos;
	private Button btn_diaMultiChos;
	private Button btn_diaProcess;
	private Button btn_diaReadProcess;
	private Button btn_diaCustom;
	private Button btn_popUpDia;
	
	private PopupWindow window=null;
	private Button cusPopupBtn1;
	private View popupView;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getView();
		setListener();
	}
	
	private void getView()
	{
		btn_diaNormal=(Button)findViewById(R.id.btn_diaNormal);
		btn_diaMulti=(Button)findViewById(R.id.btn_diaMulti);
		btn_diaList=(Button)findViewById(R.id.btn_diaList);
		btn_diaSinChos=(Button)findViewById(R.id.btn_diaSigChos);
		btn_diaMultiChos=(Button)findViewById(R.id.btn_diaMultiChos);
		btn_diaProcess=(Button)findViewById(R.id.btn_diaProcess);
		btn_diaReadProcess=(Button)findViewById(R.id.btn_diaReadProcess);
		btn_diaCustom=(Button)findViewById(R.id.btn_diaCustom);
		btn_popUpDia=(Button)findViewById(R.id.btn_popUpDia);
		
	}
	
	private void setListener()
	{
		final Button Button[] = {btn_diaNormal,btn_diaMulti,btn_diaList,
				btn_diaSinChos,	btn_diaMultiChos,btn_diaProcess,btn_diaReadProcess,
				btn_diaCustom,btn_popUpDia};
		for(int i = 0;i<9;i++){
			Button[i].setOnClickListener(btnListener);
		}
	}
	
	private Button.OnClickListener btnListener= new Button.OnClickListener()
	{
		public void onClick(View v)
		{
			if(v instanceof Button)
			{
				int btnId=v.getId();
				switch(btnId)
				{
					case R.id.btn_diaNormal:
						showNormalDia();
						break;
					case R.id.btn_diaMulti:
						showMultiDia();
						break;
					case R.id.btn_diaList:
						showListDia();
						break;
					case R.id.btn_diaSigChos:
						showSinChosDia();
						break;
					case R.id.btn_diaMultiChos:
						showMultiChosDia();
						break;
					case R.id.btn_diaReadProcess:
						showReadProcess();
						break;
					case R.id.btn_diaProcess:
						showProcessDia();
						break;
					case R.id.btn_diaCustom:
						showCustomDia();
						break;
					case R.id.btn_popUpDia:
						showCusPopUp(v);
						break;
					default:
						break;
				}
			}
		}
	};

	/*普通的對話框*/
	private void showNormalDia()
	{
		AlertDialog.Builder bui=new AlertDialog.Builder(MainActivity.this);
		bui.setIcon(R.drawable.ic_launcher);
		bui.setTitle("普通的對話框");
		bui.setMessage("這是普通對話框中的message內容");
		
		bui.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) 
				showClickMessage("您點按了確定");
			}
		});
		bui.setNegativeButton("NO", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				showClickMessage("您點按了取消");
			}
		});
		bui.create().show();//創建並顯示
	}
	
	/*多按鈕對話框*/
	private void showMultiDia()
	{
		AlertDialog.Builder multiDia=new AlertDialog.Builder(MainActivity.this);
		multiDia.setTitle("多選項對話框");
		multiDia.setPositiveButton("Button_1", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				showClickMessage("您點按了按鈕一");
			}
		});
		multiDia.setNeutralButton("Button_2", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				showClickMessage("您點按了按鈕二");
			}
		});
		multiDia.setNegativeButton("Button_3", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				showClickMessage("您點按了按鈕三");
			}
		});
		multiDia.create().show();//創建並顯示
	}
	
	
	/*列表對話框*/
	private void showListDia()
	{	
		//聲明一個存放選項的數組
		final String[] mList={"選項1","選項2","選項3","選項4","選項5","選項6","選項7"};
		AlertDialog.Builder listDia=new AlertDialog.Builder(MainActivity.this);
		listDia.setTitle("這是一個列表對話框");
		listDia.setItems(mList, new DialogInterface.OnClickListener() {//setItems(隊列對象,監聽器);
			public void onClick(DialogInterface dialog, int which) {
				/*下標是從0開始的*/
				showClickMessage(mList[which]);
			}
		});
		listDia.create().show();//創建並顯示
	}
	
	/*單項選擇對話框*/
	int yourChose;
	private void showSinChosDia()
	{	
		//聲明一個存放選項的數組
		final String[] mList={"選項1","選項2","選項3","選項4","選項5","選項6","選項7"};
		yourChose=-1;
		AlertDialog.Builder sinChosDia=new AlertDialog.Builder(MainActivity.this);
		sinChosDia.setTitle("單項選擇對話框");
		sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				yourChose=which;
				
			}
		});
		sinChosDia.setPositiveButton("確定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if(yourChose!=-1)
				{
					showClickMessage(mList[yourChose]);
				}
			}
		});
		sinChosDia.create().show();//創建並顯示
	}
	
	/*多項選擇對話框*/
	ArrayList<Integer> myChose= new ArrayList<Integer>();
	private void showMultiChosDia()
	{
		final String[] mList={"選項1","選項2","選項3","選項4","選項5","選項6","選項7"};
		final boolean mChoseSts[]={false,false,false,false,false,false,false};
		myChose.clear();//初始化數組隊列
		AlertDialog.Builder multiChosDia=new AlertDialog.Builder(MainActivity.this);
		multiChosDia.setTitle("多項選擇對話框");
		multiChosDia.setMultiChoiceItems(mList, mChoseSts, new DialogInterface.OnMultiChoiceClickListener() {
			
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				if(isChecked)
				{
					myChose.add(which);
				}
				else
				{
					myChose.remove(which);
				}
			}
		});
		multiChosDia.setPositiveButton("確定", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				int size=myChose.size();
				String str="";
				for(int i=0;i<size;i++)
				{
					str+=mList[myChose.get(i)];
				}
				showClickMessage(str);
			}
		});
		multiChosDia.create().show();
	}
	
	//進度讀取框需要模擬讀取
	ProgressDialog mReadProcessDia=null;
	public final static int MAX_READPROCESS = 100;
	private void showReadProcess()
	{
		mReadProcessDia=new ProgressDialog(MainActivity.this);
		mReadProcessDia.setProgress(0);
		mReadProcessDia.setTitle("進度條窗口");
		mReadProcessDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mReadProcessDia.setMax(MAX_READPROCESS);
		mReadProcessDia.show();
		Thread t = new Thread(this);
		t.start();
	}
	
	//開啓一個線程,循環的累加,一直到100停止
	@Override //重寫Runnable中的方法
	public void run()
	{
		int Progress= 0;
		while(Progress < MAX_READPROCESS)
		{
			try {
				Thread.sleep(100);
				Progress++;
				mReadProcessDia.incrementProgressBy(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		//讀取完後窗口自消失
		mReadProcessDia.cancel();
	}
	
	/*讀取中的對話框*/
	private void showProcessDia()
	{
		ProgressDialog processDia= new ProgressDialog(MainActivity.this);
		processDia.setTitle("進度條框");
		processDia.setMessage("內容讀取中...");
		processDia.setIndeterminate(true);
		processDia.setCancelable(true);
		processDia.show();
	}
	
	/*自定義對話框*/
	private void showCustomDia()
	{
		AlertDialog.Builder customDia=new AlertDialog.Builder(MainActivity.this);
		final View viewDia=LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_dialog, null);
		customDia.setTitle("自定義對話框");
		customDia.setView(viewDia);
		customDia.setPositiveButton("YES", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				EditText diaInput=(EditText) viewDia.findViewById(R.id.txt_cusDiaInput);
				showClickMessage(diaInput.getText().toString());
			}
		});
		customDia.create().show();
	}
	
	/*popup window 來實現*/
	private void showCusPopUp(View parent)
	{
		if(window == null)
		{
			popupView=LayoutInflater.from(MainActivity.this).inflate(R.layout.dia_cuspopup_dia, null);
			cusPopupBtn1=(Button)popupView.findViewById(R.id.diaCusPopupSure);
			window =new PopupWindow(popupView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		}
		window.setAnimationStyle(R.style.AppBaseTheme);
		/*必須調用setBackgroundDrawable, 因爲popupwindow在初始時,會檢測background是否爲null,
                  如果是onTouch or onKey events就不會相應,所以必須設置background*/
		/*彈出pop之後,不響應鍵盤事件了,這個其實是焦點在pop裏面的view去了。*/
		window.setFocusable(true);//使window失焦
		window.setBackgroundDrawable(new BitmapDrawable()); 
		window.update();
		window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0);
		cusPopupBtn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				showClickMessage("popup window的確定");
			}
		});
	}

	
	/*顯示點擊的內容*/
	private void showClickMessage(String message)
	{
		Toast.makeText(MainActivity.this, "你選擇的是: "+message, Toast.LENGTH_SHORT).show();
	}
}

佈局文件:

<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"
    android:background="@drawable/abc123"
    android:gravity="bottom"
    android:nextFocusForward="@drawable/abc123"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_diaProcess"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_diaCustom"
        android:layout_alignLeft="@+id/btn_diaCustom"
        android:text="讀取中Dialog"
        android:textColor="#faf0e6" />

    <Button
        android:id="@+id/btn_diaCustom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_popUpDia"
        android:layout_alignLeft="@+id/btn_diaReadProcess"
        android:text="自定義Dialog"
        android:textColor="#faf0e6"
       />

    <Button
        android:id="@+id/btn_diaReadProcess"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_diaProcess"
        android:layout_alignParentRight="true"
        android:text="進度條Dialog"
        android:textColor="#faf0e6" />

    <Button
        android:id="@+id/btn_diaMultiChos"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_diaReadProcess"
        android:layout_alignLeft="@+id/btn_diaReadProcess"
        android:text="多項選擇Dialog"
        android:textColor="#faf0e6" />

    <Button
        android:id="@+id/btn_diaMulti"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_diaNormal"
        android:layout_below="@+id/btn_diaNormal"
        android:text="多按鈕Dialog"
        android:textColor="#faf0e6" />

    <Button
        android:id="@+id/btn_diaList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_diaSigChos"
        android:layout_below="@+id/btn_diaMulti"
        android:text="列表Dialog"
        android:textColor="#faf0e6"
        />

    <Button
        android:id="@+id/btn_diaNormal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignTop="@+id/textView1"
        android:scrollbarStyle="outsideOverlay"
        android:text="普通Dialog"
        android:textColor="#faf0e6"
        />

    <Button
        android:id="@+id/btn_popUpDia"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_diaCustom"
        android:layout_alignParentBottom="true"
        android:text="PopUpWindow實現的dialog"
        android:textColor="#faf0e6" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/btn_diaList"
        android:layout_alignParentTop="true"
        android:text="各種Dialog合集" 
        android:textColor="#faf0e6" />

    <Button
        android:id="@+id/btn_diaSigChos"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_diaMultiChos"
        android:layout_alignLeft="@+id/btn_diaMultiChos"
        android:text="單項選擇Dialog"
        android:textColor="#faf0e6" />

</RelativeLayout>
 

    還有相應的兩個佈局文件,即自定義對話框和popup window實現的對話框中的佈局文件:

 

 

  

<?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" >

    <EditText
        android:id="@+id/txt_cusDiaInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>

 

 

<?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" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/abc123" >

        <Button
            android:id="@+id/diaCusPopupSure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_marginLeft="34dp"
            android:layout_toRightOf="@+id/button1"
            android:text="NO" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="185dp"
            android:layout_marginLeft="71dp"
            android:text="YES" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/diaCusPopupSure"
            android:layout_alignLeft="@+id/button1"
            android:layout_marginBottom="18dp"
            android:text="是否確定您的選擇?"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="50dp"
            android:text="這是通過popup window來實現"
            android:textSize="20sp" />

    </RelativeLayout>

</LinearLayout>

 

 

  至此,android中各類Dialog的實例已經全貼上去了。希望我的總結對大家有所幫助。

 

   晴時有風陰有時雨,聽說~下雨天技術博客和敲代碼更配哦。。。

 

                                                                                                                     梣梓cenzi

 

                                                                                                                    2015  4  5

  • 6f06e309-eb9a-3ed9-be22-3f71f85834dd-thumb.png
  • 大小: 964.7 KB
  • 8f9384eb-fcd2-385d-9d29-139641bc9a93-thumb.png
  • 大小: 552.2 KB
  • b67fb328-23fb-3093-9af3-0d57e3972fd9-thumb.png
  • 大小: 592.8 KB
  • 24dcf450-cfbf-3592-9387-0a3bc482f181-thumb.png
  • 大小: 331.4 KB
  • 728f9ef1-ea2b-3166-a55a-7f9b6229eb01-thumb.png
  • 大小: 301.1 KB
  • 48c3b4af-756b-3280-9811-7660a8715344-thumb.png
  • 大小: 294.2 KB
  • 7848ff91-7f2b-385b-a372-4c63d21aa6c4-thumb.png
  • 大小: 531.2 KB
  • 6be18d1f-576b-318e-84c2-a78c832876a3-thumb.png
  • 大小: 580.3 KB
  • 8bab82a4-afef-3918-93aa-dbfbbe4f009a-thumb.png
  • 大小: 557 KB
  • fecc1c06-7575-33e7-80bd-51e133712838-thumb.png
  • 大小: 983.9 KB
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章