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

猜你喜欢

转载自815222418.iteye.com/blog/2199517