alertDialog創(chuàng)建對話框

alertDialog創(chuàng)建對話框

alertDialog生成對話框可以分為以下4個區(qū)域

1、圖標區(qū)

2卵酪、標題區(qū)

3、內(nèi)容區(qū)

4谤碳、按鈕區(qū)

public class MainActivity extends Activity {

? ? private ClipboardManager show;

? ? String[] items = new String[]{

? ? ? ? ? ? "列表項1",

? ? ? ? ? ? "列表項2",

? ? ? ? ? ? "列表項3",

? ? ? ? ? ? "列表項4"

? ? };

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.alertdialog);

? ? }

? ? public void simple(View source) {

? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(this)

? ? ? ? ? ? ? ? //設置對話框標題

? ? ? ? ? ? ? ? .setTitle("簡單對話框")

? ? ? ? ? ? ? ? //設置圖標

? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)

? ? ? ? ? ? ? ? .setMessage("對話框第二行測試內(nèi)容/n");

? ? ? ? //為alterDalog.builder添加“確定”按鈕

? ? ? ? setPositiveButton(builder);

? ? ? ? //為alterDalog.builer添加取消按鈕

? ? ? ? setNegativeButton(builder).create().show();

? ? }

? ? private AlertDialog.Builder setPositiveButton(AlertDialog.Builder builder) {

? ? ? ? //調(diào)用setpostivitybutton方法添加確定按鈕

? ? ? ? return builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {

? ? ? ? ? ? ? ? show.setText("單擊了【確定】按鈕");

? ? ? ? ? ? }

? ? ? ? });

? ? }

? ? private AlertDialog.Builder setNegativeButton(AlertDialog.Builder builder) {

? ? ? ? return builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

? ? ? ? ? ? @Override

? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {

? ? ? ? ? ? ? ? show.setText("單擊了【取消】按鈕");

? ? ? ? ? ? }

? ? ? ? });

? ? }

? ? public void simpleList(View source) {

? ? ? ? AlertDialog.Builder builder = new AlertDialog.Builder(this)

? ? ? ? ? ? ? ? //設置對話框標題

? ? ? ? ? ? ? ? .setTitle("簡單列表項對話框")

? ? ? ? ? ? ? ? //設置圖標

? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)

? ? ? ? ? ? ? ? //設置簡單的列表項內(nèi)容

? ? ? ? ? ? ? ? .setItems(items, new DialogInterface.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)

? ? ? ? ? ? ? ? //設置對話框標題

? ? ? ? ? ? ? ? .setTitle("單選列表項對話框")

? ? ? ? ? ? ? ? //設置圖標

? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)

? ? ? ? ? ? ? ? //設置單選列表項溃卡,默認選中第二項(索引為1)

? ? ? ? ? ? ? ? .setSingleChoiceItems(items, 1, new DialogInterface.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)

? ? ? ? ? ? ? ? //設置對話框標題

? ? ? ? ? ? ? ? .setTitle("多選列表項對話框")

? ? ? ? ? ? ? ? //設置圖標

? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)

? ? ? ? ? ? ? ? //設置多選列表項,設置勾選第二項蜒简,第四項

? ? ? ? ? ? ? ? .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)

? ? ? ? ? ? ? ? //設置對話框標題

? ? ? ? ? ? ? ? .setTitle("自定義列表對話框")

? ? ? ? ? ? ? ? //設置圖標

? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)

? ? ? ? ? ? ? ? //設置自定義列表項

? ? ? ? ? ? ? ? .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) {

//? ? ? ? 加載界面布局文件

? ? ? ? TableLayout tl = (TableLayout) getLayoutInflater().inflate(R.layout.login, null);

? ? ? ? new AlertDialog.Builder(this)

//? ? ? ? ? ? ? ? 設置對話框圖標

? ? ? ? ? ? ? ? .setIcon(R.drawable.mia3)

//? ? ? ? ? ? ? ? 設置對話框標題

? ? ? ? ? ? ? ? .setTitle("自定義對話框")

//? ? ? ? ? ? ? ? 設置對話框的view對象

? ? ? ? ? ? ? ? .setView(tl)

//? ? ? ? ? ? ? ? 為對話框設置一個確定按鈕

? ? ? ? ? ? ? ? .setPositiveButton("登錄", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {

? ? ? ? ? ? ? ? ? ? ? ? //此處可執(zhí)行登錄處理

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? })

//? ? 為對話框設置一個取消按鈕

? ? ? ? ? ? ? ? .setNegativeButton("取消", new DialogInterface.OnClickListener() {

? ? ? ? ? ? ? ? ? ? @Override

? ? ? ? ? ? ? ? ? ? public void onClick(DialogInterface dialog, int which) {

? ? ? ? ? ? ? ? ? ? ? ? //取消登錄不進行任何操作

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? })

//創(chuàng)建并顯示對話框

? ? ? ? ? ? ? ? .create().show();

? ? }

}

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:orientation="vertical">

? ? <TextView

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="對話框"/>

? ? <Button

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:onClick="simple"

? ? ? ? android:text="消息對話框"/>

? ? <Button

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:onClick="simpleList"

? ? ? ? android:text="列表項對話框"/>

? ? <Button

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:onClick="singleChoice"

? ? ? ? android:text="單選列表對話框"/>

? ? <Button

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:onClick="multiChoice"

? ? ? ? android:text="多選列表對話框"/>

? ? <Button

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:onClick="customList"

? ? ? ? android:text="自定義列表對話框"/>

? ? <Button

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:onClick="customView"

? ? ? ? android:text="自定義view對話框"/>

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:id="@+id/loginfrom">

? ? <TableRow>

? ? ? ? <TextView

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="用戶名"

? ? ? ? ? ? android:textSize="10pt" />

? ? ? ? <!--輸入用戶名文本框-->

? ? ? ? <EditText

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:hint="請?zhí)顚懙卿涃~號"

? ? ? ? ? ? android:selectAllOnFocus="true" />

? ? </TableRow>

? ? <TableRow>

? ? ? ? <TextView

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="密碼"

? ? ? ? ? ? android:textSize="10pt" />

? ? ? ? <!--輸入密碼的文本框-->

? ? ? ? <EditText

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:hint="請?zhí)顚懨艽a"

? ? ? ? ? ? android:password="true" />

? ? </TableRow>

? ? <TableRow>

? ? ? ? <TextView

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:text="電話號碼"

? ? ? ? ? ? android:textSize="10pt" />

? ? ? ? <!--輸入電話號碼的文本框-->

? ? ? ? <EditText

? ? ? ? ? ? android:layout_width="match_parent"

? ? ? ? ? ? android:layout_height="wrap_content"

? ? ? ? ? ? android:hint="請?zhí)顚懩拿艽a"

? ? ? ? ? ? android:selectAllOnFocus="true"

? ? ? ? ? ? android:phoneNumber="true" />

? ? </TableRow>

</TableLayout>


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肤无,一起剝皮案震驚了整個濱河市阎抒,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖效拭,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異孽尽,居然都是意外死亡田巴,警方通過查閱死者的電腦和手機锡凝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門粘昨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人窜锯,你說我怎么就攤上這事张肾。” “怎么了锚扎?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵吞瞪,是天一觀的道長。 經(jīng)常有香客問我驾孔,道長芍秆,這世上最難降的妖魔是什么惯疙? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮浪听,結(jié)果婚禮上螟碎,老公的妹妹穿的比我還像新娘。我一直安慰自己迹栓,他們只是感情好掉分,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著克伊,像睡著了一般酥郭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上愿吹,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天不从,我揣著相機與錄音,去河邊找鬼犁跪。 笑死椿息,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的坷衍。 我是一名探鬼主播寝优,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼枫耳!你這毒婦竟也來了乏矾?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤迁杨,失蹤者是張志新(化名)和其女友劉穎钻心,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體铅协,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡捷沸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了狐史。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片亿胸。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖预皇,靈堂內(nèi)的尸體忽然破棺而出侈玄,到底是詐尸還是另有隱情,我是刑警寧澤吟温,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布序仙,位于F島的核電站,受9級特大地震影響鲁豪,放射性物質(zhì)發(fā)生泄漏潘悼。R本人自食惡果不足惜律秃,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望治唤。 院中可真熱鬧棒动,春花似錦、人聲如沸宾添。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽缕陕。三九已至粱锐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間扛邑,已是汗流浹背怜浅。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蔬崩,地道東北人恶座。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像沥阳,于是被迫代替她去往敵國和親跨琳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容