安卓3_彈窗AlertDialog

俗話說 實踐出真知 所以 就直接上代碼啦

1院究、activity_main.xml

很基礎(chǔ) 沒什么好說的 順著寫就好

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:ignore="HardcodedText">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World !"
        android:textSize="30sp"
        android:textColor="#159036" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="普通對話框"
        android:textSize="25sp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="單選對話框"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多選對話框"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="進度條加載"
        android:textSize="25sp"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定義彈窗"
        android:textSize="25sp"/>

</LinearLayout>
主界面.jpg

2沟娱、MainActivity.java

這里展示了5種彈窗(對話框) 的用法 但是大同小異
PS: 剛開始準備寫在一起 但是太丑了 我都看不下去 還是分開展示吧 好氣哦

0)主體
public class MainActivity extends AppCompatActivity {
    private final Context context = MainActivity.this;
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click1();
            }
        });

        button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click2();
            }
        });

        button3 = findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click3();
            }
        });

        button4 = findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click4();
            }
        });

        button5 = findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                click5();
            }
        });
    }
}
1)普通對話框
public void click1(){
    //new一個建立彈窗模板的方法
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("基礎(chǔ)彈窗");
    builder.setMessage("彈窗是否顯示成功?");
    builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(context, "啊 這募闲。笔喉。尸诽。", Toast.LENGTH_SHORT).show();
        }
    });
    builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(context, "彈窗顯示成功", Toast.LENGTH_SHORT).show();
        }
    });
    builder.show();
}
普通對話框展示.jpg
2)單選對話框
public void click2(){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("請選擇你最喜歡的選項");
    final String items[] = {"A","B","C","D","E"};
    builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String item = items[which];
            Toast.makeText(getApplicationContext(),"你選擇了:" + item.toString(),Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });
    builder.show();
}
單選對話框展示.jpg
3)多選對話框
public void click3(){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("今天做了什么?");
    final String items[] = {"吃飯","學習","睡覺","打豆豆"};
    final boolean[] checkedItems = {false,true,false,false};
    builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {}
    });
    builder.setPositiveButton("我選好了", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < checkedItems.length; i++) {
                if(checkedItems[i]){
                String option = items[i];
                sb.append(option + " ");
            }
        }
        Toast.makeText(getApplicationContext(),"今天去 " + sb.toString() + "了",Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });
    builder.show();
}
多選對話框.jpg
4)加載進度條
public void click4(){
    final ProgressDialog dialog = new ProgressDialog(context);
    dialog.setTitle("任務(wù)加載中...");
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    new Thread(){ //創(chuàng)建一個子線程
        public void run(){
            dialog.setMax(100);
            for (int i = 0; i <= 100; i++) {
                dialog.setProgress(i);
                SystemClock.sleep(50);
            }
            dialog.dismiss();
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),"加載成功",Toast.LENGTH_SHORT).show();
                    //可以同時設(shè)置多個view 或者發(fā)生消息(只允許運行在主線程的操作)
                    //安卓中的主線程叫UI線程 一個activity只允許有一個
                }
            });
        }
    }.start();
    dialog.show();
}
加載進度條展示.jpg
5)自定義彈窗
public void click5(){
    //new一個彈窗 并綁定布局
    View view1 = View.inflate(context,R.layout.layout_one,null);
    AlertDialog dialog = new AlertDialog.Builder(context)
        .setView(view1)
        .create();//只有這個方法的返回類型是 AlertDialog

    TextView textView = view1.findViewById(R.id.text1);
    //textView.setText("haha world!");
    dialog.setCanceledOnTouchOutside(false);//觸摸旁邊不會消失忽孽,但返回鍵會消失
    //dialog.setCancelable(false);//都不會消失
    dialog.show();        
}
自定義彈窗展示.jpg

3绑改、layout_one.xml

這個就是寫自定義彈窗的布局
PS: 鑒于本人水平太菜了 有些布局就強行加空格寫的 哈哈哈哈偷懶了偷懶了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:ignore="HardcodedText">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="  Hello world!"
        android:textSize="30sp"
        android:textColor="#159036"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="          "/>

        <Button
            android:id="@+id/buttonX"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="嘻嘻"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="          "/>

        <Button
            android:id="@+id/buttonH"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="哈哈"/>
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

大概就這么多了吧 還是那句話 實踐出真知! and 筆記都在注釋里!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市兄一,隨后出現(xiàn)的幾起案子厘线,更是在濱河造成了極大的恐慌,老刑警劉巖出革,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件造壮,死亡現(xiàn)場離奇詭異,居然都是意外死亡骂束,警方通過查閱死者的電腦和手機耳璧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來栖雾,“玉大人楞抡,你說我怎么就攤上這事伟众∥雠海” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵凳厢,是天一觀的道長账胧。 經(jīng)常有香客問我,道長先紫,這世上最難降的妖魔是什么治泥? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮遮精,結(jié)果婚禮上居夹,老公的妹妹穿的比我還像新娘。我一直安慰自己本冲,他們只是感情好准脂,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著檬洞,像睡著了一般狸膏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上添怔,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天湾戳,我揣著相機與錄音贤旷,去河邊找鬼。 笑死砾脑,一個胖子當著我的面吹牛幼驶,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播韧衣,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼县遣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了汹族?” 一聲冷哼從身側(cè)響起萧求,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎顶瞒,沒想到半個月后夸政,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡榴徐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年守问,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坑资。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡耗帕,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出袱贮,到底是詐尸還是另有隱情仿便,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布攒巍,位于F島的核電站嗽仪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏柒莉。R本人自食惡果不足惜闻坚,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望兢孝。 院中可真熱鬧窿凤,春花似錦、人聲如沸跨蟹。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽喷市。三九已至相种,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背寝并。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工箫措, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人衬潦。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓斤蔓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親镀岛。 傳聞我的和親對象是個殘疾皇子弦牡,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

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