Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> (Android review)dialog的使用

(Android review)dialog的使用

編輯:關於Android編程


一、基本知識點

常見的dialog


基本代碼:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog dialog = builder.create();
dialog.show();


1)常見對話框
builder.setMessage("浏覽傳智播客的網站");
builder.setPositiveButton

\

2)選擇對話框
builder.setItems(items, new DialogInterface.OnClickListener()

<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD48cD5pdGVtc8rH0ru49tGhz+7E2sjdtcTK/dfpPGJyIC8+PGJyIC8+PGltZyBzcmM9"/uploadfile/Collfiles/20140822/201408220838092.jpg" alt="\" />

3)單選對話框
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener()


第二個參數:默認選中的按鈕

\

4)多選對話框
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener()

\

5)自定義對話框
LayoutInflater mInflater = LayoutInflater.from(this);
View view = mInflater.inflate(R.layout.custom_dialog, null);


builder.setView(view);


\



二、示例代碼

1、MainActivity

package com.example.dialogtest;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

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

	public void commondialog(View view){
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setCancelable(false);
		builder.setTitle("女神");
		builder.setMessage("章澤天是你的女神嗎?");
		builder.setPositiveButton("確定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
				
			}
		});
	
		builder.setNeutralButton("隱藏", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		});
		
		builder.setNegativeButton("取消", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {

				
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public void selectdialog(View view){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		final String items[] = new String[]{
			"章澤天",
			"章澤天1",
			"章澤天2",
			"章澤天3"
		};
		builder.setTitle("選擇女神");
		builder.setItems(items, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), items[which], 0).show();
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
		
	}
	
	public void singleselectdialog(View view){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
	    
		builder.setTitle("選擇女神");
		
		final String items[] = new String[]{
			"章澤天",
			"章澤天1",
			"章澤天2",
			"章澤天3"
			
		};
		
		builder.setSingleChoiceItems(items, 0, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), items[which], 0).show();
			}
		});
		
		builder.setPositiveButton("確定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public void multiselectdialog(View view){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		final String items[] = new String[]{
			"章澤天",
			"章澤天1",
			"章澤天2",
			"章澤天3",
		};
		final boolean checkedItems[] = new boolean[]{
				true,false,false,false
		};
		
		builder.setTitle("選擇女神");
		builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				Toast.makeText(getApplicationContext(), items[which], 0).show();
			}
		}); 
		
		builder.setPositiveButton("確定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		
		
		AlertDialog dialog = builder.create();
		dialog.show();
	}
	
	public void customdialog(View view){
		LayoutInflater inflater = LayoutInflater.from(this);
		View mView = inflater.inflate(R.layout.custom_dialog, null);
		
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("請輸入用戶信息");
		builder.setView(mView);
		
		final AlertDialog dialog = builder.create();
	    dialog.show();
	    
		Button okBtn = (Button) mView.findViewById(R.id.bt_ok);
		okBtn.setOnClickListener(new View.OnClickListener() {//注意,這個demo用到了兩個OnClickListener.所以這裡需要加上View
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


三、源碼下載

http://download.csdn.net/detail/caihongshijie6/7799073


四、效果圖





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