2013-02-21 62 views
0
public void enableButton(){ 
     exitButton.setEnabled(true); 
} 
public void disableButton(){ 
     exitButton.setEnabled(false); 
} 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_second_page); 
    InputFilter[] FilterArray = new InputFilter[1]; 
    FilterArray[0] = new InputFilter.LengthFilter(4); 
    initializeEverything(); 
    text1.setFilters(FilterArray); 
    text2.setFilters(FilterArray); 
    text3.setFilters(FilterArray); 
    text4.setFilters(FilterArray); 
    text5.setFilters(FilterArray); 

    final TextView textfinal = (TextView)findViewById(R.id.finaltext); 


    TextWatcher textWatcher = new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
      textfinal.setText(calculateTotal()); 
      if(Double.parseDouble(textfinal.getText().toString())>100) 
       textfinal.setTextColor(Color.RED); 
       disableButton(); 
       Log.i("yo","gray this out"); 
      if(Double.parseDouble(textfinal.getText().toString())<100) 
       textfinal.setTextColor(Color.BLACK); 
       enableButton(); 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 

     } 
    }; 
    text1.addTextChangedListener(textWatcher); 
    text2.addTextChangedListener(textWatcher); 
    text3.addTextChangedListener(textWatcher); 
    text4.addTextChangedListener(textWatcher); 
    text5.addTextChangedListener(textWatcher); 

我有一個退出exitButton的問題。在initializeEverything()中,我設置了exitButton.setEnabled(false),它在開始時變灰。在我輸入一個小於100的數字值到editTexts(text1,text2 ...)後,該按鈕變爲啓用狀態。但是,當我超過100,應該再次禁用,但它保持啓用。 Logcat消息「grey this shout out」被髮送,所以我知道textWatcher工作正常,我不知道爲什麼按鈕不會禁用。按鈕不會在Android中禁用

+0

編輯文本爲空時,您的代碼會崩潰,因爲它無法分析空值。所以在執行邏輯之前檢查條件 – moDev 2013-02-21 19:17:04

+0

我有數據獲取方法中檢查空值的if語句 – lonewaft 2013-02-22 18:02:01

回答

4

問題是這些線路

if(Double.parseDouble(textfinal.getText().toString())>100) 
      textfinal.setTextColor(Color.RED); 
      disableButton(); 
      Log.i("yo","gray this out"); 
if(Double.parseDouble(textfinal.getText().toString())<100) 
      textfinal.setTextColor(Color.BLACK); 
      enableButton(); 

請注意,只有一條線連接到if如果你不使用支架。將這些行替換爲:

if(Double.parseDouble(textfinal.getText().toString())>100){ 
      textfinal.setTextColor(Color.RED); 
      disableButton(); 
      Log.i("yo","gray this out"); 
}else if(Double.parseDouble(textfinal.getText().toString())<100){ 
      textfinal.setTextColor(Color.BLACK); 
      enableButton(); 
} 
+0

哦,我現在看到了,好的謝謝。 – lonewaft 2013-02-21 19:17:26

+0

@lonewaft接受這個答案....謝謝你不會榮幸他的答案.. – Pragnani 2013-02-21 20:00:21