iT邦幫忙

2021 iThome 鐵人賽

DAY 24
0
自我挑戰組

才30天?一個月學會Android開發系列 第 24

Day-24 AlertDialog

AlertDialog與Toast皆可用於顯示訊息,

但與Toast不同的是,

AlertDialog可與使用者間進行互動,

常用於確認使用者是否執行此步驟。


以下是AlertDialog對話方塊常用的方法

  • setTitle():標題
  • setIcon():圖示
  • setMessage():顯示內容
  • setItems():以表列顯示內容
  • setPositiveButton():加入右邊按鈕
  • setNegativeButton():加入中間按鈕
  • setNeutralButton():加入左邊按鈕
    註:setPositiveButton、setNegativeButton、setNeutralButton為三個位置固定的按鈕

而AlertDialog的語法如下

AlertDialog.Builder 變數名稱 = new AlertDialog.Builder(主程式類別);
變數名稱.setTitle(標題);
變數名稱.stIcon(圖示);
變數名稱.setMessage(內容);
變數名稱.按鈕名稱(按鈕文字, new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
          執行內容              
     }
});
變數名稱.show();

以下為簡單的範例,在畫面中央設置離開Button,id為exit

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="結束"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.alertdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button exit;

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

        exit = findViewById(R.id.exit);

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("確認視窗");
                builder.setMessage("確定要離開APP?");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });
                builder.show();
            }
        });
    }
}

為Button加入監聽事件,觸發內容為顯示AlertDialog,
並為AlertDialog設定Title、Message及兩個Button分別是確定及取消,
在確定的執行內容裡打上finish()表示按下會結束APP,
最後打上show即完成此範例。
https://ithelp.ithome.com.tw/upload/images/20211006/201419504e3562gSlA.png


上一篇
Day-23 Toast
下一篇
Day-25 ImageView
系列文
才30天?一個月學會Android開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言