2016-02-18 65 views
0

我是Java和Android Studio的初学者,所以在这里我的问题是:我创建了一个活动的警告对话窗口,其中正面的按钮是“OK”,负面的按钮是“不,谢谢”。如下面的代码所示。如何修改AlertDialog(Android)?

if(Times==0) { 
     AlertDialog.Builder builder1 = new AlertDialog.Builder(this); 
     builder1.setIcon(android.R.drawable.ic_dialog_alert); 
     builder1.setTitle("Warning"); 
     builder1.setMessage("Rooting of a phone may void your Warranty in most of the cases,so it is adviced to proceed at your own risk"); 
     builder1.setCancelable(true); 

     builder1.setPositiveButton(
       "OK", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         Times += 1; 
         dialog.cancel(); 
        } 
       }); 

     builder1.setNegativeButton(
       "No Thanks", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.cancel(); 

        } 
       }); 
} 

眼看就要罚款,但现在的前提条件是我想如果用户单击“确定”按钮,并不愿如果用户点击“OK”再次显示它显示只有一次。我在我的课程中创建了一个变量times,并将其初始化为0,如下所示。

public class rootingRooting extends AppCompatActivity { 

int Times=0; 

,并把完成AlertDialog中,如果循环,增加它的价值,当用户点击“确定”,这样的循环可能只执行一次如果用户点击“确定”,但它是没有用每当我点击“确定”即可打开警报框正在显示的活动。所以,现在我想要做的事情是:

  1. 如果用户点击“确定”,则不应显示警告框。

  2. 如果用户点击了“不,谢谢”按钮,我想带他去家里活动。那么,我应该如何使用“不谢谢”按钮的意图?

谢谢。

回答

3

您需要使用SharedPreferenes来持久保存数据,本地变量将无济于事。 这样的事情:

编辑根据您的要求,我添加了一个示例活动类来显示整个过程。查看评论之间更多信息

EDIT 2看到代码后//其次编辑点评

public class MainActivity extends AppCompatActivity { 
     SharedPreferences prefs; 
      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
       //When the activity starts, we look into the shared prefs and get an int of name "ok_clicked" from it. 
       //0 will be the default value of the int if there is no int stored in sharedPreferences. 
       prefs = getSharedPreferences("myPrefs", MODE_PRIVATE); 
       int times = prefs.getInt("ok_clicked", 0); 
       //if the times value is 0, we will open the dialog, otherwise nothing happens 
       if (times==0){ 
        openDialog(); 
       } 
      } 
      //Read This comment First: We will create a Method, which create an alert Dialog. 
      private void openDialog(){ 
       AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
       dialog.setTitle("Test").setMessage("Lorem ipsum dolor"); 
       dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         //When OK button is clicked, an int with value of 1 will be saved in sharedPreferences. 
         prefs = getSharedPreferences("myPrefs", MODE_PRIVATE); 
         SharedPreferences.Editor editor = prefs.edit(); 
         editor.putInt("ok_clicked", 1); 
         editor.apply(); 
        } 
       }); 
       dialog.setNegativeButton("No Thanks", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
        //Second Edit: To open another acitivty on No Thanks Button 
        Intent intent = new Intent(MyActivity.this, HomeActivity.class);         
        startActivity(intent); 
        } 
       }); 
       dialog.show(); 
      } 
     } 
+0

谢谢医生威尔逊,但作为Java中的新手,以及Android的,我只是不知道我在哪里使用了我期望你更具体一些的东西,坦率地说,这是我听说sharedPreferences的第一次,并且在我的课程宣布之后,我尝试了一下第一个模块,导入所需的文件,但仍然显示为“无法解析符号putInt”或“无法解析符号适用”谢谢.. –

+0

不要把第一个BL在活动声明之后,将其放入AlertDialog的“确定”按钮的OnClick中。并把其他两个块代替你的if(时间== 0),当你调用AlertDialog – drWisdom

+0

谢谢drWilson.I想通了,现在它正在工作,因为我试了它,还有一件事是第二件事问题我问了我现在如何使用“不,谢谢”按钮的意图,如果用户点击将直接带他们回家或其他活动。请您也请回答这一点? –