2016-02-18 22 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想通了,現在它正在工作,因爲我試了它,還有一件事是第二件事問題我問了我現在如何使用「不,謝謝」按鈕的意圖,如果用戶點擊將直接帶他們回家或其他活動。請您也請回答這一點? –