如何建立帶有“中性”選項的對話方塊?


此示例演示如何建立帶有中性選項的對話方塊。

步驟 1 - 在Android Studio中建立一個新專案,轉到檔案⇒新建專案,並填寫所有必需的詳細資訊以建立一個新專案。

步驟 2 - 將以下程式碼新增到res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/parent"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity"
   android:gravity="center"
   android:orientation="vertical">
   <Button
      android:id="@+id/customDialog"
      android:text="Custom Dialog"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
</LinearLayout>

在上面的程式碼中,我們添加了一個按鈕。當用戶點選按鈕時,它將顯示對話方塊。

步驟 3 - 將以下程式碼新增到src/MainActivity.java

package com.example.andy.myapplication;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.customDialog).setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.CustomAlertDialog);
            builder.setTitle("Alert Dialog");
            builder.setMessage("Lorem Ipsum is simply dummy text of the printing and typesetting industry.
               Lorem Ipsum has been the industry's standard dummy text ever since the 1500s");
            builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked yes", Toast.LENGTH_LONG).show();
               }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked No", Toast.LENGTH_LONG).show();
               }
            });
            builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Toast.makeText(MainActivity.this, "You have clicked Neutral", Toast.LENGTH_LONG).show();
               }
            });
            final AlertDialog alertDialog = builder.create();
            alertDialog.show();
         }
      });
   }
}

在上面的程式碼中,當用戶點選按鈕時,它將顯示帶有“是”、“中性”和“否”按鈕的對話方塊。當用戶點選“是”按鈕時,它將顯示“是”訊息;“否”按鈕顯示“否”訊息;“中性”按鈕顯示中性訊息。

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要在Android Studio中執行應用程式,請開啟專案中的一個活動檔案,然後點選執行 工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -

在上面的結果中,顯示了初始螢幕。現在點選按鈕,它將開啟帶有“是”、“否”和“中性”按鈕的對話方塊。

現在點選“中性”按鈕,它將給出如下所示的輸出 -

點選此處下載專案程式碼

更新於:2019年7月30日

362 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.