2017-12-23 51 views
0

アンドロイドアプリの初めての使用時に表示されるダイアログを設定しました。このダイアログにはボタンがあります。ボタンを送信するとこのダイアログを非表示にする方法 ここに私のコードダイアログボタン初めての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

答えて

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

+0

ここで私はMainActivity内のダイアログを呼び出しますが、別のxmlの中にダイアログを実装しています –

+0

@ Demon.r updated answer – Curio

関連する問題