2016-07-27 105 views
0

我正在创建一个关于我们的屏幕作为一个弹出菜单/对话框在Android中。我想添加一个按钮(OK或CANCEL)到这个对话框。我怎样才能做到这一点 ?在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);。但是我没有在对话框中显示任何按钮。

现在,当我触摸屏幕上的任何位置时,对话框会关闭。我只想通过确定按钮关闭弹出窗口。

在此先感谢!

输出

enter image description here

回答

1

下面是我的代码添加按钮。

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

您的按钮不来了,因为它应该是这样的

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

尝试,让我知道

+0

难道ü尝试上述一个 – Priyanka

+0

@AbhishekTandon设置'dialog.setCancelable(假)'在屏幕上的点击禁用对话框解聘。 – SripadRaj