【问题标题】:Custom Dialog with do not show this again自定义对话框不再显示
【发布时间】:2019-07-21 06:45:10
【问题描述】:

我遇到了自定义对话框的问题,我创建了自定义对话框,它可以正常工作,但是如果用户单击按钮,我对不再显示它感到困惑。就像用户单击该按钮时有一个名为 rating 的按钮,然后自定义对话框将永远不会再次打开。所以如果有人知道怎么做,请帮助我

这是我的代码

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

        }
    });

}

【问题讨论】:

    标签: java android dialog android-alertdialog


    【解决方案1】:

    如果您试图确定用户是否在 Playstore 中对您的应用进行了评分,那么没有合适的方法可以这样做,并且有充分的理由。如果应用能够确定对应用进行评分的用户,他们就可以对评分高的用户提供奖励,对评分低的用户进行“惩罚”。

    一个好的经验法则是,

    1. 让用户使用该应用 5 次,以获得良好的感觉。
    2. 在第 6 次运行时提示用户使用 YesLaterNever 选项对其进行评分。稍后将其延迟“n”(您的选择)天。

    现在,要确定用户之前是选择了Yes 还是Never,您可以将值存储在SharedPreference 中。

    在 Preference 中设置值:

    // 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

    【讨论】:

      【解决方案2】:

      如果他点击按钮,您可能需要写入文件来存储变量

      这是读取文件:

      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) { }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多