【发布时间】:2016-07-27 17:41:04
【问题描述】:
我正在创建一个关于我们的屏幕作为 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);。但我没有在对话框中显示任何按钮。
现在,当我触摸屏幕上的任意位置时,对话框会关闭。我只想通过确定按钮关闭弹出窗口。
提前致谢!
输出
【问题讨论】:
标签: android android-studio android-alertdialog android-button