日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺(tái) 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > 互聯(lián)網(wǎng) > Adrnoid開發(fā)系列(二十五):使用AlertDialog創(chuàng)建各種類型的對話框

Adrnoid開發(fā)系列(二十五):使用AlertDialog創(chuàng)建各種類型的對話框

來源:程序員人生   發(fā)布時(shí)間:2014-11-07 08:37:22 閱讀次數(shù):2056次

AlertDialog可以生成各種內(nèi)容的對話框,但是每種對話框都會(huì)有這類的結(jié)構(gòu):


類似下邊這類的:


這只是最簡單的對話框。


我們來看下創(chuàng)建1個(gè)對話框需要的步驟:

1、使用創(chuàng)建AlertDialog.Builder對象

2、調(diào)用AlertDialog.Builder的setTitle()或setCustomTitle()方法設(shè)置標(biāo)題

3、調(diào)用AlertDialog.Builder的setIcon()方法設(shè)置圖標(biāo)

4、調(diào)用1些其他設(shè)置方法設(shè)置標(biāo)題

5、調(diào)用AlertDialog.Builder的setPositiveButton()、setNegativeButton()或setNeutralButton()添加多個(gè)按鈕

6、調(diào)用create()方法創(chuàng)建AlertDialog對象,再調(diào)用AlertDialog對象的show()方法將該對話框顯示出來。


新建Android項(xiàng)目,然后編寫main.xml:

<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <!-- 顯示1個(gè)普通的文本編輯框組件 --> <EditText android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false"/> <!-- 定義1個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="簡單對話框" android:onClick="simple" /> <!-- 定義1個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="簡單列表項(xiàng)對話框" android:onClick="simpleList" /> <!-- 定義1個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="單選列表項(xiàng)對話框" android:onClick="singleChoice" /> <!-- 定義1個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多選列表項(xiàng)對話框" android:onClick="multiChoice" /> <!-- 定義1個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定義列表項(xiàng)對話框" android:onClick="customList" /> <!-- 定義1個(gè)普通的按鈕組件 --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定義View對話框" android:onClick="customView" /> </LinearLayout>
這里是定義了6個(gè)按鈕和1個(gè)文本顯示框,并且設(shè)置了相應(yīng)的onClick屬性


接下來,我們就要編寫主界面的java代碼:AlertDialogTest.java

package org.crazyit.ui; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.TableLayout; import android.widget.TextView; public class AlertDialogTest extends Activity { TextView show; String[] items = new String[] { "瘋狂Java講義", "瘋狂Ajax講義", "輕量級(jí)Java EE企業(yè)利用實(shí)戰(zhàn)", "瘋狂Android講義" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); show = (TextView) findViewById(R.id.show); } public void simple(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對話框標(biāo)題 .setTitle("這是對話框標(biāo)題") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) .setMessage("這是對話框內(nèi)容"); // 為AlertDialog.Builder添加【肯定】按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加【取消】按鈕 setNegativeButton(builder) .create() .show(); } public void simpleList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對話框標(biāo)題 .setTitle("簡單列表項(xiàng)對話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置簡單的列表項(xiàng)內(nèi)容 .setItems(items, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你選中了《" + items[which] + "》"); } }); // 為AlertDialog.Builder添加【肯定】按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加【取消】按鈕 setNegativeButton(builder) .create() .show(); } public void singleChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對話框標(biāo)題 .setTitle("單選列表項(xiàng)對話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置單選列表項(xiàng),默許選中第2項(xiàng)(索引為1) .setSingleChoiceItems(items, 1, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("你選中了《" + items[which] + "》"); } }); // 為AlertDialog.Builder添加【肯定】按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加【取消】按鈕 setNegativeButton(builder) .create() .show(); } public void multiChoice(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對話框標(biāo)題 .setTitle("多選列表項(xiàng)對話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置多選列表項(xiàng),設(shè)置勾選第2項(xiàng)、第4項(xiàng) .setMultiChoiceItems(items , new boolean[]{false , true ,false ,true}, null); // 為AlertDialog.Builder添加【肯定】按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加【取消】按鈕 setNegativeButton(builder) .create() .show(); } public void customList(View source) { AlertDialog.Builder builder = new AlertDialog.Builder(this) // 設(shè)置對話框標(biāo)題 .setTitle("自定義列表項(xiàng)對話框") // 設(shè)置圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置自定義列表項(xiàng) .setAdapter(new ArrayAdapter<String>(this , R.layout.array_item , items), null); // 為AlertDialog.Builder添加【肯定】按鈕 setPositiveButton(builder); // 為AlertDialog.Builder添加【取消】按鈕 setNegativeButton(builder) .create() .show(); } public void customView(View source) { //裝載/res/layout/login.xml界面布局 TableLayout loginForm = (TableLayout)getLayoutInflater() .inflate( R.layout.login, null); new AlertDialog.Builder(this) // 設(shè)置對話框的圖標(biāo) .setIcon(R.drawable.tools) // 設(shè)置對話框的標(biāo)題 .setTitle("自定義View對話框") // 設(shè)置對話框顯示的View對象 .setView(loginForm) // 為對話框設(shè)置1個(gè)“肯定”按鈕 .setPositiveButton("登錄" , new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 此處可履行登錄處理 } }) // 為對話框設(shè)置1個(gè)“取消”按鈕 .setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 取消登錄,不做任何事情。 } }) // 創(chuàng)建、并顯示對話框 .create() .show(); } private AlertDialog.Builder setPositiveButton( AlertDialog.Builder builder) { // 調(diào)用setPositiveButton方法添加肯定按鈕 return builder.setPositiveButton("肯定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("單擊了【肯定】按鈕!"); } }); } private AlertDialog.Builder setNegativeButton( AlertDialog.Builder builder) { // 調(diào)用setNegativeButton方法添加取消按鈕 return builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("單擊了【取消】按鈕!"); } }); } }
在這里邊,第5個(gè)和第6個(gè)按鈕用到了兩個(gè)樣式:array_item.xml和login.xml


我們看下他們的內(nèi)容:

array_item.xml:

<?xml version="1.0" encoding="utf⑻"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:textColor="#f0f" android:textSize="30dp" android:shadowColor="#ff0" android:shadowRadius="2" android:shadowDx="5" android:shadowDy="5" android:layout_width="match_parent" android:layout_height="wrap_content" />

login.xml:

<?xml version="1.0" encoding="utf⑻"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/loginForm" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="用戶名:" android:textSize="10pt" /> <!-- 輸入用戶名的文本框 --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請?zhí)顚懙卿泿ぬ?hào)" android:selectAllOnFocus="true" /> </TableRow> <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="密碼:" android:textSize="10pt" /> <!-- 輸入密碼的文本框 --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請?zhí)顚懨艽a" android:password="true" /> </TableRow> <TableRow> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="電話號(hào)碼:" android:textSize="10pt" /> <!-- 輸入電話號(hào)碼的文本框 --> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請?zhí)顚懩碾娫捥?hào)碼" android:selectAllOnFocus="true" android:phoneNumber="true" /> </TableRow> </TableLayout>

通過AlertDialog可以制作出不同風(fēng)格的對話框,在很多時(shí)候都比較有用

并且我們可以通過肯定按鈕來把數(shù)據(jù)通過Intent傳遞到另外1個(gè)界面中。


生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 欧美视频网 | 97福利电影| 怡红院在线观看 | 国产精品免费一区二区三区 | 最新中文字幕在线视频 | 免费a级毛片在线播放 | 欧美一区二区三区电影 | 亚洲欧美自拍视频 | 久久久精品久久久 | 亚洲成人av电影 | 婷婷av在线 | 国产精品国产三级国产普通话三级 | 在线视频中文字幕 | 国产精品自在线 | 国产日韩欧美在线观看 | 成人免费在线观看 | 中文字幕在线观看日韩 | 污视频网站在线免费观看 | 高清久久久 | 亚洲国产成人精品久久 | 久久精品一区二区三区不卡牛牛 | 99久久精品国产一区二区三区 | 色呦呦视频在线观看 | 国产精品国产三级国产专播品爱网 | 天堂√中文最新版在线 | 国产成人精品一区二区三区在线 | 黄色av播放 | 国产中文字幕在线 | 久久亚洲精品小早川怜子66 | 亚洲一区二区三区免费视频 | 国产最新视频 | 午夜精品久久久久久久99无限制 | 国产一区二区三区在线免费看 | 日韩电影在线 | 亚洲精品乱码久久久久久蜜桃动漫 | 久久久久成人免费 | 国产一区二区三区的电影 | 亚洲免费观看 | 精品伦精品一区二区三区视频 | 午夜视频成人 | 亚洲色图35p |