【问题标题】:Dialog Button First Use for Android App首次用于 Android 应用程序的对话框按钮
【发布时间】:2017-12-23 16:02:59
【问题描述】:

我已经配置了一个对话框在第一次使用android应用程序时显示,并且这个对话框包含按钮,一旦我提交按钮如何隐藏这个对话框 这是我的代码

MainActivity 代码

 private boolean isFirstTime() {
    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    boolean ranBefore = preferences.getBoolean("RanBefore", false);
    if (!ranBefore) {
        //show dialog if app never launch
        dialog.show();
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("RanBefore", true);
        editor.commit();

    }
    return !ranBefore;
}

//Create Dialog
    dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.dialog_user);
    //method call

user_dialog 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="453dp"
        android:background="@null"
        android:src="@drawable/first_login" />

</LinearLayout>
<Button
    android:id="@+id/btnok"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="20dip"
    android:text="OK, GOT IT"
    android:background="@null"
    android:textColor="#0000FF"
    android:textSize="20sp" />

once click on OK,GOT IT to be dimess

【问题讨论】:

    标签: android xml android-studio button dialog


    【解决方案1】:

    请记住,您必须在对话框中执行 findViewById,因为按钮在对话框中。

        final Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.dialog_user);
        Button okButton = (Button) dialog.findViewById(R.id.btnok);
        okButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                dialog.dismiss();
            }
        });
        dialog.show();
    

    但是,可以通过其他方式删除对话框,例如在对话框外部单击或单击后退箭头。如果您只想使用按钮删除对话框,则必须添加:dialog.setCancelable(false);

    【讨论】:

    • 在哪里添加它,我在 MainActivity 中调用对话框,而我已经在另一个 xml 中实现了对话框
    猜你喜欢
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 2023-03-09
    • 2023-03-30
    相关资源
    最近更新 更多