【问题标题】:Displaying alert dialog only after a specific activity page仅在特定活动页面之后显示警报对话框
【发布时间】:2013-04-09 07:12:46
【问题描述】:

我有一个登录页面。当用户正确登录时,我想显示一个警告对话框,说明您的登录详细信息已经过验证。单击继续继续。现在我希望在下一个活动中显示此警告对话框登录后的页面。 这是警报对话框代码-:

SharedPreferences 设置 = getSharedPreferences(PREFS_NAME, 0); boolean dialogShown = settings.getBoolean("dialogShown", false);

     if (!dialogShown) {
           AlertDialog.Builder builder = new Builder(Myperwallet.this);
           builder.setTitle("Fast Cashier!");
           builder.setMessage("Logging In");
           builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   dialog.cancel();
       //continue activity here....

               }
           });  

           builder.create().show();
             // AlertDialog code here


     }



       SharedPreferences.Editor editor = settings.edit();
       editor.putBoolean("dialogShown", true);
       editor.commit(); 

我试过这个,但现在我注销后不会显示警告对话框。我需要知道如何在按下注销按钮时将标志设置为 true。这是我的注销按钮功能-:

 logout.setOnClickListener(new OnClickListener() {   
              public void onClick(View v) {

                AlertDialog alertDialog = new AlertDialog.Builder(Myperwallet.this).create(); 
                alertDialog.setTitle("FastCashier:");
                alertDialog.setMessage("Are you sure you want to exit?");
                alertDialog.setButton( Dialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {

                       Intent logout = new Intent(Myperwallet.this,Login.class);
                       startActivity(logout);

                   }
                   });

                alertDialog.setButton( Dialog.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener()    {
                  public void onClick(DialogInterface dialog, int which) {

                      dialog.cancel();

                  }
                  });

                alertDialog.show();  
              }
            });

【问题讨论】:

  • 您需要在第二个Activity中再次编写代码

标签: android dialog


【解决方案1】:

您需要在当前代码中添加一些实现。
您可以在 sharedPrefs 中保留一个标志,最初它是正确的。在第一次显示后将其设为假。
这是您现在必须实施的。

所以要在注销后再次显示它,当用户注销时将标志重置为真。因此,当您再次到达活动时,将显示对话框。

【讨论】:

  • 嘿,亲爱的,你能告诉我如何重置标志吗?请参阅上面我编辑的代码。
【解决方案2】:

实际上无法解决更多问题,但是您想在某些Optionnale Requirement 中显示您的AlertDialog,例如当Login 显示时,下一页是Required 但由于@987654326 @ 在应用程序启动时仅显示一次。当我注销然后登录时,我没有收到任何alert dialog

SharedPreferences 所做的就是用密钥存储您的特定 Sting,您需要与之匹配。在 loing 成功后,您需要 Update SharedPreferences 使用一些新字符串并需要与之匹配。

您也可以使用File 来检查isExit() 与否并编写require Sting 它并阅读它并匹配您的require 条件以显示alert dialog。这可能对你有帮助。

【讨论】:

    【解决方案3】:

    跟踪活动流程,即当您启动应用程序时,它正在执行 onCreate 方法中的操作,为您在 on create 方法中设置问题标志,以及当您从任何其他活动切换到此实现时Restart() 上的方法并清除您以前的数据.... 像这样……

    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
    
        //ur code
    }
    

    希望对你有帮助,

    【讨论】:

      猜你喜欢
      • 2022-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2020-01-14
      相关资源
      最近更新 更多