0

このフォームのメソッドを使用して、アプリのすべてのアラート ウィンドウを定義する Alerts クラスがあります。

   public static void Alert1(Context con) {
            AlertDialog.Builder builder = new AlertDialog.Builder(con);
            builder.setTitle("my title");
            builder.setIcon(android.R.drawable.ic_dialog_info);
            DialogListner listner = new DialogListner();
            builder.setMessage("my message");
            builder.setPositiveButton("ok", listner);

            AlertDialog diag = builder.create();
            diag.show();
    }

同様の方法で、チェックボックス「今後これを表示しない」を選択した場合はこのアラートの表示を避けるアラートウィンドウも作成したいと思います

4

4 に答える 4

3

この質問に対する明快で正しいアプローチがあります

package com.example.user.testing;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {
CheckBox dontShowAgain;
public static final String PREFS_NAME = "MyPrefsFile1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
    LayoutInflater adbInflater = LayoutInflater.from(MainActivity.this);
    View eulaLayout = adbInflater.inflate(R.layout.checkbox, null);

    dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
    adb.setView(eulaLayout);
    adb.setTitle("Attention");
    adb.setMessage("Your message here");
    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {


            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("skipMessage", dontShowAgain.isChecked());
            editor.commit();
            dialog.cancel();
        }
    });

    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    Boolean skipMessage = settings.getBoolean("skipMessage", false);
    if (skipMessage.equals(false)) {
        adb.show();
    }

}

}

于 2016-05-31T18:16:55.220 に答える
1

アプリケーションSharedPreferenceオブジェクトを使用できます。初めて読んでみて、共有設定にアイテムを追加します。あなたがしなければならないのは、オブジェクトに値を入れて、毎回それをチェックすることだけです。

于 2012-12-26T14:18:47.883 に答える
0

このチェックボックスとテキストビューを使用して、ダイアログ用に個別のレイアウトを作成できます。このレイアウトを膨らませて、ダイアログのビューとして設定を追加します

さて、それは以下のようなものになるでしょう。

View view = layoutInflator.inflate(R.layout.dialogView,null);
//and set to your dialog
dialog.setView(view);

//Or you can set it to your builder also

チェックボックスのonCheckListenerを処理します

于 2012-12-26T14:18:12.470 に答える
0

他の人が示唆したように、目的のメッセージとコントロール (チェックボックス) を含むカスタム レイアウトを作成しSharedPreferences、ユーザーの選択を追跡するために使用する必要があります。

これらのトピックについて説明する 2 つのブログ投稿を次に示します。SharedPreferences から
の Android の書き込みと読み取りでのカスタム ダイアログの作成と表示 (免責事項: 私のブログ)

于 2012-12-26T14:27:16.317 に答える