Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中常用的對話框總結

Android中常用的對話框總結

編輯:關於Android編程

把常用的對話框記一下,方便以後查用

布局文件



    

代碼實現

package com.wzw.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	/**
	 * 確定取消對話框
	 * @param v
	 */
	public void click1(View v){
		AlertDialog.Builder builder=new Builder(this);
		builder.setTitle("我是標題");
		builder.setMessage("我是提示信息");
		builder.setPositiveButton("確定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), "確定被點擊啦", 0).show();
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				//	Toast.makeText(getApplicationContext(), "取消被點擊啦", 0).show();
				// 什麼都不寫,默認的是關閉對話框
			}
		});
		builder.create().show();
		
	}
	/**
	 * 單選對話框
	 * @param v
	 */
	public void click2(View v){
		AlertDialog.Builder builder=new Builder(this);
		builder.setTitle("我是標題");
		
		final String items[]={"男","女","屌絲"};
		builder.setSingleChoiceItems(items, 2, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), "性別是"+items[which], 0).show();
				dialog.dismiss();
			}
		});
		
		
		
		
		builder.create().show();
	}
	
	/**
	 * 多選對話框
	 * @param v
	 */
	public void click3(View v){
		AlertDialog.Builder builder=new Builder(this);
		builder.setTitle("我是標題");
		
		final String[] items={"蘋果","香蕉","栗子","西瓜"};
		final boolean[] result=new boolean[]{true,false,true,false};
		builder.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), items[which]+isChecked, 0).show();
				result[which]=isChecked;
			}
		});
		builder.setPositiveButton("提交", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				StringBuffer sb=new StringBuffer();
				for (int i = 0; i < result.length; i++) {
					if(result[i]){
						sb.append(items[i]+"--");
					}
				}
				Toast.makeText(getApplicationContext(), "您的選擇"+sb.toString(), 0).show();
			}
		});
		
		
		
		builder.create().show();
	}
	
	
	public void click4(View v){
		ProgressDialog pd=new ProgressDialog(this);
		pd.setTitle("提示信息");
		pd.setMessage("正在加載數據,請稍等");
		pd.show();
		
	}
	
	
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved