java - Android 中的按钮不会禁用

标签 java android button

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);

我在退出按钮变灰时遇到问题。在initializeEverything()中,我设置了exitButton.setEnabled(false),它在开始时呈灰色。在我向 editTexts(text1, text2 ...) 输入一些小于 100 的数值后,按钮将启用。但是当我超过 100 时,它应该再次被禁用,但它保持启用状态。发送 Logcat 消息“灰色这个狗屎”,所以我知道 textWatcher 工作正常,我不确定为什么按钮不会禁用。

最佳答案

问题出在这些行

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();
}

关于java - Android 中的按钮不会禁用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15010329/

相关文章:

java - 将对象注入(inject)过滤器

android - Unity Android 应用将不再登录 Google Play

javascript - 按钮不适用于简单的 html Clicker 游戏

Android开发-按钮 react 慢

Java Swing : adding button to TitledBorder

java - 通过 GWT RPC 传递类对象的问题

java - 在 Java 类中实现的 UML 类图关联

android - LoaderManager.onLoadFinished 即使在特定查询与更新不匹配时也会被调用

android - 在 android 默认选项卡式 Activity 中, fragment 不适合屏幕,因为高度和 actionBar 是可移动的

android - 从另一个 Activity 启用按钮