簡體   English   中英

自定義對話框,不再顯示

[英]Custom Dialog with do not show this again

我在使用自定義對話框時遇到問題,我創建了自定義對話框,它可以正常工作,但是我對如果用戶單擊按鈕后不再顯示它感到困惑。 就像有一個名為“評分”的按鈕,當用戶單擊該按鈕時,自定義對話框將永遠不會再次打開。 所以,如果有人知道該怎么做,請幫助我

這是我的代碼

   final Dialog dialogrul = new Dialog(MainActivity.this);
    dialogrul.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    Window window = dialogrul.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    dialogrul.setContentView(R.layout.exitlayout);
    Button rating = dialogrul.findViewById(R.id.ratingok);
    Button dialogok = dialogrul.findViewById(R.id.exityes);
    final Button dialognotok = dialogrul.findViewById(R.id.exitno);

    dialogrul.setCancelable(true);
    dialogrul.show();
    dialogok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MainActivity.this.onSuperBackPressed();
            finish();

        }
    });
    rating.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent j = new Intent(android.content.Intent.ACTION_VIEW);
            j.setData(Uri.parse("https://play.google.com/store/apps/details?"));
            startActivity(j);
        }
    });
    dialognotok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialogrul.cancel();

        }
    });

}

如果您要確定用戶是否在Playstore中為您的應用程序評分,則沒有適當的方法,這是有充分理由的。 如果應用程序能夠確定對應用程序進行評分的用戶,則他們可以為高評分提供獎勵,而為低評分提供“懲罰”。

取而代之的是,

  1. 讓用戶使用該應用5次,以獲得良好的使用體驗。
  2. 在第6次運行時提示用戶使用“ Yes ,“ Later和“ Never選項對其進行評分。 稍后將其延遲“ n”(您選擇的)天。

現在,要確定用戶Yes選擇Yes還是Never ,可以將值存儲在SharedPreference

在首選項中設置值:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("rating_choice", user_choice);
 editor.apply();

從首選項中檢索數據:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
String name = prefs.getString("rating_choice", "Later");//"Later" is the default value.

參考文獻: 123

如果他單擊按鈕,可能要寫一個文件來存儲一個變量

這是讀取文件:

private String readFromFile(Context context) {

        String ret = "";

        try {
            InputStream inputStream = context.openFileInput("yourfilename.txt");

            if ( inputStream != null ) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receiveString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ( (receiveString = bufferedReader.readLine()) != null ) {
                    stringBuilder.append(receiveString);
                }

                inputStream.close();
                ret = stringBuilder.toString();
            }
        }
        catch (FileNotFoundException e) { } catch (IOException e) { }

        return ret;
    }

這是要寫入文件:

private void writeToFile(String data,Context context) {
        String x=readFromFile(this);
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("yourfilename.txt", Context.MODE_PRIVATE));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
        }
        catch (IOException e) { }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM