2012-11-10 26 views
1

編輯:下面的代碼正常工作!錯誤是其他地方在我的代碼...動態更改AlertDialog中按鈕的文本

原單後如下...

編輯結束。

我有一個textView加載onResume()與onClickListener()打開AlertDialog。 AlertDialog有一些文本和三個按鈕,setPositivtButton,setNegativeButton和setNeutralButton。每次AlertDialog打開時,我都想根據某些變量更改中性按鈕的文本。我已經試過這沒有成功...

enter code here 
protected void OnResume() { 
    TextView text = new TextView (this); 
    text.setTextColor(res.getColor(R.color.white)); 
    text.setOnClickListener(new OnClickListener() { 
     public void onClick(final View v) {     
      final String str = (String) v.getTag(); 
      String buttonText = "Jump"; 
      AlertDialog.Builder showinf = new AlertDialog.Builder(v.getContext()); 
      showinf.setTitle("SomeTitle"); 
      showinf.setMessage("someMeassage"); 
      showinf.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 
      if (variable == true) buttonText = "Sit"; 
       else buttonText = "Jump"; 
      variable = !variable; //For testing purposes... 
      showinf.setNeutralButton(buttonText, new DialogInterface.OnClickListener()  {       
       public void onClick(DialogInterface dialog, int id) { 
        // Do something 
       } 
      }); 
      showinf.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // Do something 
       } 
      }); 
      showinf.show(); 
     }; 
    }); 
} 

是它在某種程度上可能與一些tweeks這個代碼,還是我不得不重寫整個事情? AlertDialog應該在每次單擊textView時創建,還是可以直接在setNeutralButton代碼中執行?

編輯:

修改一下(變量=變量!)並沒有什麼變化的代碼。 也試圖用一個對話框沒有成功...

我是新來的android,但沒有設置TextView和Dialog在onResume()使它不可能改變任何東西,直到再次使用onClickListener加載onResume()作爲我完成了嗎?

- 湯米

+0

你確定'變量'布爾值正在改變? – st0le

+0

是的。甚至像這樣設置變量變量:variable =!variable; – user1086500

回答

-1

創建一個單獨的類來保存ü希望的值。然後訪問它使用該單例類的實例