Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> AndroidUI組件之AlertDialog

AndroidUI組件之AlertDialog

編輯:關於Android編程

package com.gc.alertdialogdemo;
/**
 * AlertDialog:
 * 1、AlertDialog的功能很強大,它可以生成各種內容的對話框,實際上AlertDialog生成的
 * 的對話框總可以分為4個區域:(1)圖標區(2)標題區(3)內容區(3)按鈕區。
 * 2、創建一個對話框需要經過如下幾個步驟:
 * (1)使用創建AlertDialog.Builder對象。
 * (2)調用AlertDialog.Builder的setTitle()或setCustomTitle()方法設置標題
 * (3)調用AlertDialog.Builder的setIcon()方法設置標題
 * (4)調用AlertDialog.Builder的相關設置方法設置對話框內容
 * (5)調用AlertDialog.Builder的setPositiveButton()、setNegativeButton()或setNeutralButton()方法添加多個按鈕
 * (6)調用AlertDialog.Builder的create()方法創建AlertDialog對象,再調用AlertDialog對象的show()方法將該對話框顯示出來。
 * 3、AlertDialog提供了如下6種方法來指定對話框內容:
 * (1)setMessage():設置對話內容為簡單文本內容。
 * (2)setItems():設置對話框內容為簡單列表項
 * (3)setSingleChoiceItems():設置對話框內容為單選列表項
 * (4)setMultiChoiceItems():設置對話框內容為多選列表項
 * (5)setAdapter():設置對話框內容為自定義列表項
 * (6)setView():設置對話框內容為自定義View.
 * 
 */
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
/**
 * 
 * @author Android將軍
 *
 */
public class MainActivity extends Activity {

	public TextView show;
	private String[] items=new String[]
			{
			"Android將軍",
			"ios將軍",
			"Cocos2d-x將軍",
			"將軍"
			};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		show=(TextView)findViewById(R.id.show);
	}
	public void customView(View source)
	{
		TableLayout loginForm=(TableLayout)getLayoutInflater().inflate(R.layout.login, null);
		new AlertDialog.Builder(this)
		.setTitle("自定義View對話框")
		//設置圖標
		.setIcon(R.drawable.ic_launcher)
		.setView(loginForm)
		.setPositiveButton("登錄", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		})
		.setNegativeButton("取消", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}
		})
		.create()
		.show();
	}
	public void customList(View source)
	{
		AlertDialog.Builder builder=new AlertDialog.Builder(this)
		//設置對話框標題
		.setTitle("自定義列表對話框")
		//設置圖標
		.setIcon(R.drawable.ic_launcher)
		.setAdapter(new ArrayAdapter(this, R.layout.array_item, items), null);
		setPositiveButton(builder);
		setNeutralButton(builder);
		//為AlertDialog.Builder添加“取消”按鈕
		setNegativeButton(builder).create().show();
	}
	public void multiChoice(View source)
	{
		AlertDialog.Builder builder=new AlertDialog.Builder(this)
		//設置對話框標題
				.setTitle("多選列表對話框")
				//設置圖標
				.setIcon(R.drawable.ic_launcher)
				.setMultiChoiceItems(items, new boolean[]{false,true,false,true}, null);
		setPositiveButton(builder);
		setNeutralButton(builder);
		//為AlertDialog.Builder添加“取消”按鈕
		setNegativeButton(builder).create().show();
	}
	public void singleChoice(View source)
	{
		AlertDialog.Builder builder=new AlertDialog.Builder(this)
		//設置對話框標題
				.setTitle("單選列表對話框")
				//設置圖標
				.setIcon(R.drawable.ic_launcher)
				.setSingleChoiceItems(items, 1, new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						show.setText("你選中了《"+items[which]+"》");
					}
				});
		setPositiveButton(builder);
		setNeutralButton(builder);
		//為AlertDialog.Builder添加“取消”按鈕
		setNegativeButton(builder).create().show();
		
	}
	public void simpleList(View source)
	{
		AlertDialog.Builder builder=new AlertDialog.Builder(this)
		//設置對話框標題
		.setTitle("簡單列表對話框")
		//設置圖標
		.setIcon(R.drawable.ic_launcher)
		//設置簡單的列表項內容
		.setItems(items, new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				show.setText("你選中了《"+items[which]+"》");
				
			}
		});
		setPositiveButton(builder);
		setNeutralButton(builder);
		//為AlertDialog.Builder添加“取消”按鈕
		setNegativeButton(builder).create().show();
	}
	public void simple(View source)
	{
		AlertDialog.Builder builder=new AlertDialog.Builder(this)
		//設置對話框標題
		.setTitle("簡單對話框")
		//設置圖標
		.setIcon(R.drawable.ic_launcher)
		.setMessage("對話框的測試內容\n第二行內容");
		//為AlertDialog.Builder添加“確定”按鈕
		setPositiveButton(builder);
		setNeutralButton(builder);
		//為AlertDialog.Builder添加“取消”按鈕
		setNegativeButton(builder).create().show();
		
	}
	private Builder setNeutralButton(Builder builder) {
		// TODO Auto-generated method stub

		return builder.setNeutralButton("修飾", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				show.setText("單擊了【修飾】按鈕!");
			}
		});
	}
	private Builder setNegativeButton(Builder builder) {
		// 調用setNegativeButton方法添加“確定”按鈕
		return builder.setNegativeButton("取消", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				show.setText("單擊了【取消】按鈕!");
			}
		});
	}
	private Builder setPositiveButton(Builder builder) {
		
		
		// 調用setPositiveButton方法添加“確定”按鈕
		return builder.setPositiveButton("確定", new OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				show.setText("單擊了【確定】按鈕!");
			}
		});
	}

	
}




幾個布局文件如下:
activity_main.xml:



      
   


login.xml



    
        
        
        
    
    
        
        
        
    
    
        
        
        
    





array_item.xml



    




部分效果截圖:
\

\


轉載請注明出處:http://blog.csdn.net/android_jiangjun/article/details/25739587

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