跟我學Android之十 對話框

視頻課:https://edu.csdn.net/course/play/7621
android

本章內容app

第1節  Toast提示框
第2節  AlertDialog對話框
第3節  特點對話框
第4節  自定義對話框ide

本章目標佈局


熟練掌握 Toast 的用法。ui

熟練掌握 Dialog 的用法。this

掌握幾種經常使用的特點對話框的用法。spa

掌握自定義對話框的方法。.net

掌握 Notification 的用法。設計



Toast 是一種浮於屏幕最上層,用於顯示消息的窗體,u 主要用於顯示各類動做的提示信息,好比:3d

Ø 正在編寫的郵件自動保存到了草稿箱

Ø 刪除某條通信了成功了

u Toast 窗體只是佔據一部分的屏幕窗口

u 當前的 Activity 依然保持存在並能與用戶交互

u Toast 在顯示一段時間超時後會自動關閉





構建 Toast 消息

u 1 、經過 Toast.makeText () 方法建立一個 Toast 對象

Context context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast  toast = Toast.makeText(context, text, duration);


Ø duration 參數表示 Toast 顯示的超時時間設定,能夠取值以下:

l Toast.LENGTH_SHORT 短期顯示(大約 2 秒)

l Toast.LENGTH_LONG 長時間顯示(大約 3.5 秒)

u 2 、經過 Toast.show () 方法顯示 Toast 窗體

u


調整 Toast 的顯示位置

u 默認狀況下, Toast 顯示在屏幕底部,水平居中的位置

u 經過 Toast.setGravity () 方法能夠修改顯示位置,支持三個參數

Ø 第一個參數 gravity :接收一個 Gravity 常量,能夠是常量的組合

Ø 第二個參數是 x 方向的偏移量

Ø 第三個參數是 y 方向的偏移量

toast .setGravity(Gravity.TOP|Gravity.LEFT, 20, 30);


Toast 的外觀除了默認狀況外,也能夠進行定製

u 1 、爲 Toast 中顯示的內容定製一個視圖佈局( toast_layout.xml 



<LinearLayout   ……><TextView android:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#FFF"</LinearLayout>



u 2 、採用動態加載的辦法在程序中爲 Toast 使用定製佈局



LayoutInflater li = getLayoutInflater();View layout = li.inflate(R.layout.toast_layout_root, null);TextView tv = (TextView)layout.findViewById(R.id.text);tv.setText("custom toast");				Toast t = new Toast(getApplicationContext());t.setGravity(Gravity.CENTER, 0, 0);t.setDuration(Toast.LENGTH_SHORT);t.setView(layout);t.show();



Dialog 是一個讓用戶作一個決定或者輸入一些信息的窗口, 對話框一般不充滿屏幕,  Toast 不一樣,對話框不會自動關閉, 對話框出現時,當前 Activity 的界面繼續顯示但不能進行交互, 對話框不是一個視圖,基類是 Dialog 繼承自 Object




對話框的建立方法有多種

u Android 系統提供了不少對話框的類用於建立各類對話框

Ø android.app.AlertDialog

Ø android.app.DatePickerDialog

Ø android.app.TimePickerDialog

Ø android.app.ProgressDialog

u 自主定義對話框內容

u 經過 AlertDialog 建立對話框是最經常使用的辦法



AlertDialog  Dialog 的直接子類

u 包含一個標題

u 包含一個文本消息

u 能夠管理 0  3 個按鈕

u 能夠包含單選列表和多選列表

u 不能直接建立對象,須要使用 AlertDialog.Builder

u

u



使用 AlertDialog

u 建立 AlertDialog 對象



AlertDialog.Builder builder = new AlertDialog.Builder(this);AlertDialog alert = builder.create();



u 使用 Builder 對象的 setTitle () 方法設置標題內容



builder.setTitle(「信息提示」);


u 使用 Builder 對象的 setMessage () 方法設置消息內容



builder.setMessage (「信息提示」);


使用 AlertDialog, 添加按鈕



builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {MyActivity.this.finish();}}).setNegativeButton("No", new  DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {dialog.cancel();}});



簡單列表對話框至關於將 ListView 組件放在對話框上

u 經過 AlertDialog.Builder 中的 setItems () 設置內容和事件監聽



AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("請選擇你最喜歡的運動");builder.setItems(items, new OnClickListener() {	public void onClick(DialogInterface dialog, int which) {		show.setText("你選中了《" + items[which] + "》");	}});builder.create().show();



簡單列表對話框至關於將 ListView 組件放在對話框上, 運行後的效果圖




單選列表對話框用於顯示單選列表

u 經過 Builder 中的 setSingleChoiceItems () 設置內容和事件監聽




AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setSingleChoiceItems(items, 1, new OnClickListener(){			public void onClick(DialogInterface dialog, int which)	{	show.setText("你選中了《" + items[which] + "》");	}	});builder.setTitle("請選擇要使用的情景模式");builder.create().show();



單選列表對話框用於顯示單選列表, 運行後的效果圖





複選列表對話框用於顯示覆選項列表, 經過 Builder 中的 setMultiChoiceItems () 設置內容和事件監聽




final boolean[] checkedItems= new boolean[] { false, true, false,true, false };AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("請選擇您喜好的遊戲");builder.setMultiChoiceItems(items, checkedItems,new OnMultiChoiceClickListener() {	public void onClick(DialogInterface dialog,	int which, boolean isChecked) {	checkedItems[which]=isChecked;	//改變被操做列表項的狀態	}});builder.create().show();



複選列表對話框用於顯示覆選項列表

u 運行後的效果圖





 定義 列表對話框用於顯示自定義列表, 經過 Builder 中的 setAdapter () 設置列表項內容




final String[] items = new String[] { "邁巴赫","布加迪","法拉利","保時捷"};AlertDialog.Builder builder = new AlertDialog.Builder(this)	.setTitle("自定義列表項對話框")	.setIcon(R.drawable.tools)	.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items), new OnClickListener() {					public void onClick(DialogInterface dialog, int which) {	show.setText("你選中了《" + items[which] + "》");	}});builder.create().show();



自定義列表對話框用於顯示自定義列表, 運行後的效果圖




自定義對話框的實現思路

u 所謂自定義就是要擺脫對話框的固有模式,顯示自定義的設計

u 首先須要作的是經過佈局的方式將自定義內容的佈局設計出來

Ø 通常會在 res/layout 下經過 xml 文件存放自定義的設計

u 而後經過對話框提供的能力將自定義的佈局顯示在對話框中

u AlertDialog 類提供了使用自定義內容的基礎




經過 AlertDialog 類實現自定義對話框

u AlertDialog  Dialog 的子類,徹底具有了 Dialog 的特性

u 對於 Dialog 來說,若是沒有設置 Title  Title 位置依然空白存在

Ø AlertDialog 能夠解決這個問題

u 使用 AlertDialog 加載自定義佈局的示例以下:




LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(R.layout.custom_dlg, null);AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setView(layout);AlertDialog alertDialog = builder.create();alertDialog.show();




自定義對話框, 示例運行效果



對話框風格的窗口


這種對話樞本質上依然是窗口,只是把顯示窗口的 Activity 的風格設爲對話樞風格即 可, 須要 先增長 Activity, 在清單文件中進行設置




<activity	android:name="com.aaa.ui.DialogTheme"	android:theme="@android:style/Theme.Dialog"	android:label="@string/app_name">	<intent-filter>	<action android:name="android.intent.action.MAIN" />	<category android:name="android.intent.category.LAUNCHER" />	</intent-filter></activity>



自定義對話框

u 示例運行效果


相關文章
相關標籤/搜索