兄弟連雲Android學習筆記——Dialog(對話框)

學習對話框Dialog

1.提示對話框     

2.普通列表對話框                                                  

3. 單選對話框                                       

4. 多選對話框                                     

5.自定義視圖對話框

MainAcitivity.java

package com.example.dialog;

import android.R.anim;
import android.R.layout;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity implements android.view.View.OnClickListener{
	
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private Button btn5;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1 = (Button) findViewById(R.id .button1); //獲取Button
		btn1.setOnClickListener(this);  //設置單擊事件
		btn2 = (Button) findViewById(R.id .button2); //獲取Button
		btn2.setOnClickListener(this);  //設置單擊事件
		btn3 = (Button) findViewById(R.id .button3); //獲取Button
		btn3.setOnClickListener(this);  //設置單擊事件
		btn4 = (Button) findViewById(R.id .button4); //獲取Button
		btn4.setOnClickListener(this);  //設置單擊事件
		btn5 = (Button) findViewById(R.id .button5); //獲取Button
		btn5.setOnClickListener(this);  //設置單擊事件
	}

	@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;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			createAlertDialog1();
			break;

		case R.id.button2:
			createAlertDialog2();
			break;
			
		case R.id.button3:
			createAlertDialog3();
			break;
		case R.id.button4:
			createAlertDialog4();
			break;
		case R.id.button5:
			createAlertDialog5();
			break;

		default:
			break;
		}
	}
	
	//自定義視圖對話框
	private void createAlertDialog5() {
	AlertDialog.Builder builder = new AlertDialog.Builder(this);
	builder.setTitle("登錄");
	builder.setIcon(android.R.drawable.ic_dialog_info);
	
	//設置自定義視圖
	LayoutInflater inflater = getLayoutInflater(); //佈局填充器
//	LayoutInflater inflater = LayoutInflater.from(this);
	
	//實例化佈局組件
	final View view = inflater.inflate(R.layout.login,null);  //view 相當於LinearLayout
	
	builder.setView(view);
	
	builder.setPositiveButton("登錄", new DialogInterface.OnClickListener() {
		
		public void onClick(DialogInterface dialog, int which) {
			
		EditText et_username = (EditText)view.findViewById(R.id.editText1_username);
		EditText et_password = (EditText)view.findViewById(R.id.editText2_password);
		String username = et_username.getText().toString();
		String password = et_password.getText().toString();
		
		Toast.makeText(MainActivity.this, username+"--"+password, Toast.LENGTH_SHORT).show();
		}
	});
	
	builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		
		public void onClick(DialogInterface dialog, int which) {	
			
		}
	});
	//中立按鈕
	builder.setNeutralButton("隱藏", new DialogInterface.OnClickListener() {
		
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			
		}
	});
	builder.show();
	}

	//多選對話框
	private void createAlertDialog4() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final String[] names = {"蘋果","草莓"};
		builder.setTitle("選幾個吧");
		
		//參數(數據列表,默認選中的選項null表示沒有,事件處理)
		builder.setMultiChoiceItems(names, null, new DialogInterface.OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				java.lang.System.out.println("which:"+which);
				java.lang.System.out.println("isChecked:"+isChecked);
			}
		});
		
		builder.show();
	}

	
	//單選對話框
	private void createAlertDialog3() {
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final String[] names = {"蘋果","草莓"};
		builder.setTitle("選擇一個");
		//參數(數據列表,默認選中索引-1表示沒有選中,事件處理)
		builder.setSingleChoiceItems(names, -1, new DialogInterface.OnClickListener() {
		
			public void onClick(DialogInterface dialog, int arg1) {
				Toast.makeText(MainActivity.this, names[arg1], Toast.LENGTH_LONG).show();
				//關閉對話框
				dialog.dismiss();
			//	dialog.cancel();
			}
		});
		builder.show();
	}

	//創建普通列表選擇的對話框
	private void createAlertDialog2() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		final String[] names = {"蘋果","草莓"};
		
		builder.setTitle("選擇一個");
		//設置選項列表
		builder.setItems(names, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, names[which], Toast.LENGTH_LONG).show();	
			}
		});
		builder.show();
	}

	//創建一個提示信息的對話框
	private void createAlertDialog1() {
	
		//創建一個AlertDialog的建構對象
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("刪除");//設置標題
		builder.setMessage("你真的要刪除嗎");//提示信息
		
		//設置對話框不能被取消
		builder.setCancelable(false);
		//設置正面按鈕
		builder.setPositiveButton("確定",new DialogInterface.OnClickListener() {
					
			public void onClick(DialogInterface arg0, int arg1) {
				//單擊確定後的事件處理
				Toast.makeText(MainActivity.this, "已刪除", Toast.LENGTH_LONG).show();
			}
		});
		//設置反面按鈕
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "操作已取消", Toast.LENGTH_LONG).show();
			}
		});
	
		//顯示對話框
		builder.show();
	}

}
提示對話框                                                          普通列表對話框                                           單選對話框                       多選對話框                                      自定義視圖對話框




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章