簡體   English   中英

在Android中創建警報對話框並在對話框(彈出)中添加一個按鈕以關閉它

[英]Creating Alert Dialog box in Android and add a button to Dialog (popup) to close it

我正在創建一個關於我們屏幕作為 android 中的彈出窗口/對話框。 我想在此對話框中添加一個按鈕(確定或取消)。 我該怎么做?

這是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:id="@+id/popup"
    android:layout_height="wrap_content"
    android:background="#E3C39D"
    android:orientation="vertical"
    android:padding="0dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="About Us.."
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="8dp"
        android:textSize="20dp"
        android:textColor="#ffffff"
        style="@style/TextShadow"/>

    <View
        android:id="@+id/SplitLine_hor1"
        android:layout_width="match_parent"
        android:layout_height= "1dp"
        android:background="#000" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="16dp"
        android:text="Hello ! I want to put a button below with label 'OK' and Click on this OK button the popup should be close. Thank you !" />

</LinearLayout>

下面是對話框的功能

public void AboutUsDialog(){
        final AlertDialog.Builder alert;
        alert = new AlertDialog.Builder(this);
        LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.activity_about_us, null);
        alert.setView(dialogView);
        alert.show();

        alert.setPositiveButton("OK",null);
        //alert.setInverseBackgroundForced(true);
    }

我正在使用alert.setPositiveButton("OK",null); alert.setInverseBackgroundForced(true); . 但是我沒有在對話框中顯示任何按鈕。

現在,當我觸摸屏幕上的任何地方時,對話框都會關閉。 我只想通過 OK 按鈕關閉彈出窗口。

提前致謝!

輸出

在此處輸入圖片說明

你的按鈕不會出現,因為它應該是這樣的

alert.setPositiveButton("OK",null);
alert.setView(dialogView);
        alert.show();

而不是

alert.setView(dialogView);
        alert.show();
alert.setPositiveButton("OK",null);

參考這個:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
      alertDialogBuilder.setMessage("Are you sure,You wanted to make decision");

      alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface arg0, int arg1) {
            Toast.makeText(MainActivity.this,"You clicked yes button",Toast.LENGTH_LONG).show();
         }
      });

      alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
            finish();
         }
      });

      AlertDialog alertDialog = alertDialogBuilder.create();
      alertDialog.show();

嘗試讓我知道

下面是我添加按鈕的代碼。

public void AboutUsDialog() {
        final AlertDialog.Builder alert;
        alert = new AlertDialog.Builder(this);
        LayoutInflater inflater = MainActivity.this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.activity_about_us, null);
        alert.setPositiveButton("OK", null);  //This is my Solution to this question(adding OK button)
        alert.setCancelable(false);
        alert.setInverseBackgroundForced(true);
        alert.setView(dialogView);
        alert.show();
        //alert.setInverseBackgroundForced(true);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM