AlertDialog各種對(duì)話框的用法實(shí)例

各種對(duì)話框展示圖



首先講解AlerDialog的構(gòu)造過(guò)程:

AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

//首先構(gòu)造AlerDialog的構(gòu)造器

builder.setTitle("標(biāo)題");//設(shè)置標(biāo)題

builder.setIcon(R.mipmap.ic_launcher_round);//設(shè)置圖標(biāo)大小;

builder.setMessage("這是輸入框的內(nèi)容");

//在對(duì)話框中添加一個(gè)確認(rèn)按鈕;

builder.setPositiveButton ("確認(rèn)",new DialogInterface.OnClickListener () {

@Override

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

Toast.makeText ( MainActivity.this,"點(diǎn)擊了確定按鈕",Toast.LENGTH_SHORT ).show ();

}

} );

AlertDialog dialog=builder.create ();//實(shí)例化AlerDialog類

dialog.show ();//顯示該對(duì)話框


接著講解AlerDialog常用按鈕形式:1.確認(rèn)按鈕:builder.setPositiveButton(...)? ?2.取消按鈕builder.setNegativeButton 3.中立按鈕builder.setNeutralButton(...)


接下來(lái)講解實(shí)現(xiàn)全過(guò)程:



MainActivity的頁(yè)面布局(簡(jiǎn)單的幾個(gè)按鈕)

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

? ? xmlns:app="http://schemas.android.com/apk/res-auto"

? ? xmlns:tools="http://schemas.android.com/tools"

? ? android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? tools:context=".MainActivity"

? ? android:orientation="vertical">

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="50dp"

? ? ? ? android:id="@+id/sure"

? ? ? ? android:text="確認(rèn)對(duì)話框"

? ? ? ? android:layout_gravity="center"/>

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="50dp"

? ? ? ? android:id="@+id/single"

? ? ? ? android:text="單選對(duì)話框"

? ? ? ? android:layout_gravity="center"/>

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="50dp"

? ? ? ? android:id="@+id/more_opt"

? ? ? ? android:text="多選對(duì)話框"

? ? ? ? android:layout_gravity="center"/>

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="50dp"

? ? ? ? android:id="@+id/list"

? ? ? ? android:text="列表對(duì)話框"

? ? ? ? android:layout_gravity="center"/>

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="50dp"

? ? ? ? android:id="@+id/define"

? ? ? ? android:text="自定義對(duì)話框"

? ? ? ? android:layout_gravity="center"/>


接下來(lái)主頁(yè)面代碼:

package com.mingrisoft.alertdialog;

import android.content.Context;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

import android.widget.Toolbar;

import java.sql.BatchUpdateException;

import androidx.appcompat.app.AlertDialog;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivityextends AppCompatActivityimplements View.OnClickListener {

private ButtonmSure;

private ButtonmSingle;

private ButtonmMoreOpt;

private ButtonmList;

private ButtonmDefine;

//以下是下面多選框或者列表框所要存儲(chǔ)的數(shù)據(jù),采取java中Container(容器類)來(lái)存儲(chǔ);

? ? private String[]sexList={"男","女"};//單選列表

? ? private String[]likeList={"籃球","足球","排球","聽(tīng)音樂(lè)","看電影"};//多選列表

? ? private String[]itemList={"項(xiàng)目經(jīng)理","策劃","Android工程師","電工","美工"};//列表

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

super.onCreate ( savedInstanceState );

setContentView ( R.layout.activity_main );

initView ();

}

private void initView() {

mSure = (Button) findViewById ( R.id.sure );

mSingle = (Button) findViewById ( R.id.single );

mMoreOpt = (Button) findViewById ( R.id.more_opt );

mList = (Button) findViewById ( R.id.list );

mDefine = (Button) findViewById ( R.id.define );

mSure.setOnClickListener (this );

mSingle.setOnClickListener (this );

mMoreOpt.setOnClickListener (this );

mList.setOnClickListener (this );

mDefine.setOnClickListener (this );

}

@Override

? ? public void onClick(View v) {

switch (v.getId ()) {

case R.id.sure:

showDialog1();//確認(rèn)對(duì)話框

? ? ? ? ? ? ? ? break;

case R.id.single:

showDialog2();//單選對(duì)話框

? ? ? ? ? ? ? ? break;

case R.id.more_opt:

showDialog3();//多選對(duì)話框

? ? ? ? ? ? ? ? break;

case R.id.list:

showDialog4();//列表對(duì)話框

? ? ? ? ? ? ? ? break;

case R.id.define:

showDialog5();//自定義對(duì)話框

? ? ? ? ? ? ? ? break;

}

}

private void showDialog1() {

AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );

builder.setTitle ("購(gòu)物車" );

builder.setIcon ( R.mipmap.ic_launcher );

builder.setMessage ("是否購(gòu)買該物品" );

builder.setPositiveButton ("確認(rèn)",new DialogInterface.OnClickListener () {

@Override

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

Toast.makeText ( MainActivity.this,"點(diǎn)擊了確定按鈕",Toast.LENGTH_SHORT ).show ();

}

} );

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

@Override

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

Toast.makeText(MainActivity.this,"點(diǎn)擊了取消按鈕",Toast.LENGTH_SHORT).show();

}

} );

builder.setNeutralButton ("讓我再想想",new DialogInterface.OnClickListener () {

@Override

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

Toast.makeText ( MainActivity.this,"待會(huì)再買",Toast.LENGTH_LONG ).show ();

}

} );

AlertDialog dialog=builder.create ();

dialog.show ();

}

private void showDialog2() {

AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );

builder.setTitle ("性別:" );

builder.setIcon ( R.mipmap.android );

builder.setSingleChoiceItems (sexList, -1,new DialogInterface.OnClickListener () {

@Override

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

String sex=sexList[which];

Toast.makeText ( MainActivity.this,"這個(gè)性別為"+sex,Toast.LENGTH_SHORT ).show ();

}

} );

//設(shè)置中立事件以及點(diǎn)擊事件

? ? ? ? builder.setNeutralButton ("性別暫時(shí)不清楚",new DialogInterface.OnClickListener () {

@Override

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

Toast.makeText ( MainActivity.this,"暫時(shí)不知道性別",Toast.LENGTH_LONG ).show ();

}

} );

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

@Override

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

Toast.makeText ( MainActivity.this,"這是您的選擇",Toast.LENGTH_SHORT ).show ();

}

} );

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

@Override

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

dialog.dismiss ();//關(guān)閉對(duì)話框

? ? ? ? ? ? }

} );

AlertDialog dialog=builder.create ();

dialog.show ();

}

private void showDialog3() {

AlertDialog.Builder builder =new AlertDialog.Builder ( MainActivity.this );

builder.setTitle ("個(gè)人愛(ài)好" );

builder.setIcon ( R.mipmap.android );

builder.setMultiChoiceItems (likeList,null,new DialogInterface.OnMultiChoiceClickListener () {

@Override

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

if (isChecked) {

Toast.makeText ( MainActivity.this,"我喜歡" +likeList[which], Toast.LENGTH_SHORT ).show ();

}else {

Toast.makeText ( MainActivity.this,"我不喜歡" +likeList[which], Toast.LENGTH_SHORT ).show ();

}

}

} );

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

@Override

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

Toast.makeText ( MainActivity.this,"這是您的選擇", Toast.LENGTH_SHORT ).show ();

}

});

//設(shè)置“中立”按鈕夏伊,及點(diǎn)擊事件

? ? ? ? builder.setNeutralButton ("等等",new DialogInterface.OnClickListener () {

@Override

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

Toast.makeText ( MainActivity.this,"讓我想一想",Toast.LENGTH_LONG ).show ();

}

} );

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

@Override

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

dialog.dismiss ();//關(guān)閉對(duì)話框

? ? ? ? ? }

} );

AlertDialog dialog=builder.create ();

dialog.show ();//顯示對(duì)話框

? ? }

private void showDialog4() {

AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );

builder.setTitle ("職業(yè)崗位" );

builder.setIcon ( R.mipmap.android );

builder.setItems (itemList,new DialogInterface.OnClickListener () {

@Override

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

Toast.makeText ( MainActivity.this,"你的職業(yè)"+itemList[which],Toast.LENGTH_SHORT ).show ();

}

} );

AlertDialog dialog=builder.create ();

dialog.show ();//顯示對(duì)話框

? ? }

private void showDialog5() {

//自定義對(duì)話框

? ? ? ? LayoutInflater inflater=LayoutInflater.from (this );

View view=inflater.inflate ( R.layout.self_define,null );

AlertDialog.Builder builder=new AlertDialog.Builder ( MainActivity.this );

builder.setTitle ("自定義布局" );

builder.setView ( R.layout.self_define );

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

@Override

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

Toast.makeText ( MainActivity.this,"確定選擇",Toast.LENGTH_LONG ).show ();

}

} );

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

@Override

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

Toast.makeText ( MainActivity.this,"取消選擇",Toast.LENGTH_LONG ).show ();

}

} );

AlertDialog dialog=builder.create ();

dialog.show ();//顯示對(duì)話框

? ? }

}


https://www.jb51.net/article/83192.htm


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末谨究,一起剝皮案震驚了整個(gè)濱河市像屋,隨后出現(xiàn)的幾起案子谐算,更是在濱河造成了極大的恐慌沈撞,老刑警劉巖蜈垮,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異菊碟,居然都是意外死亡节芥,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門逆害,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)头镊,“玉大人,你說(shuō)我怎么就攤上這事魄幕∠嗤В” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵纯陨,是天一觀的道長(zhǎng)坛芽。 經(jīng)常有香客問(wèn)我,道長(zhǎng)翼抠,這世上最難降的妖魔是什么咙轩? 我笑而不...
    開(kāi)封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮阴颖,結(jié)果婚禮上活喊,老公的妹妹穿的比我還像新娘。我一直安慰自己量愧,他們只是感情好钾菊,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著偎肃,像睡著了一般煞烫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上软棺,一...
    開(kāi)封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天红竭,我揣著相機(jī)與錄音,去河邊找鬼喘落。 笑死,一個(gè)胖子當(dāng)著我的面吹牛最冰,可吹牛的內(nèi)容都是我干的瘦棋。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼暖哨,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼赌朋!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤沛慢,失蹤者是張志新(化名)和其女友劉穎赡若,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體团甲,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡逾冬,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了躺苦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片身腻。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖匹厘,靈堂內(nèi)的尸體忽然破棺而出嘀趟,到底是詐尸還是另有隱情,我是刑警寧澤愈诚,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布她按,位于F島的核電站,受9級(jí)特大地震影響炕柔,放射性物質(zhì)發(fā)生泄漏尤溜。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一汗唱、第九天 我趴在偏房一處隱蔽的房頂上張望宫莱。 院中可真熱鬧,春花似錦哩罪、人聲如沸授霸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)碘耳。三九已至,卻和暖如春框弛,著一層夾襖步出監(jiān)牢的瞬間辛辨,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工瑟枫, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留斗搞,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓慷妙,卻偏偏與公主長(zhǎng)得像僻焚,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子膝擂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355

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