0

Ich erstelle einen About us-Bildschirm als Popup/Dialog in Android. Ich möchte eine Schaltfläche (OK oder ABBRECHEN) zu diesem Dialog hinzufügen. Wie kann ich das machen ?Erstellen von Alarmdialogfeld in Android und Hinzufügen einer Schaltfläche zu Dialog (Popup), um es zu schließen

Das ist meine Layout-Datei

<?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> 

und unten ist die Funktion für Dialogfeld

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); 
    } 

I alert.setPositiveButton("OK",null); oder alert.setInverseBackgroundForced(true); verwenden. Aber ich habe keine Schaltfläche im Dialog angezeigt bekommen.

Jetzt Dialog wird deaktiviert, wenn ich irgendwo auf dem Bildschirm berühren. Ich möchte das Popup nur über die OK-Schaltfläche schließen.

Vielen Dank im Voraus!

Ausgabe

enter image description here

Antwort

1

Unten ist mein Code zu Schaltflächen hinzufügen.

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); 
    } 
0

Ihre Taste kommt nicht, weil es wie dieses

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

Statt

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

Siehe dies sein sollte:

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(); 

Versuch und lassen Sie mich wissen

+0

Did u die oben ein – Priyanka

+0

versuchen '@AbhishekTandon Set' dialog.setCancelable (false) Dialog entlasse auf Klicken der Bildschirm zu deaktivieren. – SripadRaj