繁体   English   中英

1 个 EditText 的 2 个 TextWatchers

[英]2 TextWatchers for 1 EditText

我正在为自己开发一个简单的 android 应用程序项目。 我发现了一篇让我决定使用 TextWatcher 的文章。 很简单,我将 TextWatcher 添加到我的代码中。 但事情是这样的。 我设计了 2 个 RadioButtons 和 1 个 EditText。 两个 RadioButton 都将决定我的唯一一个 EditText 的格式。

这是我希望复制应用程序的方式:

  1. 如果用户按下按钮 A,则 Edittext 的输入格式将如下所示:

    ABCD 1234 EFGH 5678

    所以,它每 4 个字符有一个空格

  2. 如果用户按下按钮 B,则 EditText 的输入格式将如下所示:

    ABCD1234EFGH5678

    因此,每 4 个字符中没有空格

即使用户再次按下反义词,EditText 的格式也会遵循条件

这是我试过的:

使用 if-else 条件,甚至创建 2 个 TextWatcher,但流程仍然是这样的:

制作 2 TextWatchers 来确定案例,但仍然是我所拥有的:

  1. 用户按下按钮 B,Edittext 格式将类似于ABCD1234EFGH5678
  2. 然后,用户按下按钮 A,Edittext 格式将类似于ABCD 1234 EFGH 5678
  3. 当用户再次按下按钮 B 时,Edittext 格式将类似于ABCD 1234 EFGH 5678而不是ABCD1234EFGH5678

我该怎么做才能达到这种方法?

这是我的 MainViewGenerator.java:

this.editTextNumberSpaceTextWatcher = new EditTextNumberSpaceTextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        super.onTextChanged(s, start, before, count);
        editTextString = s.toString();
    }
};

this.editTextNumberNormalTextWatcher = new EditTextNumberNormalTextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        super.onTextChanged(s, start, before, count);
        editTextString = s.toString().replaceAll(" ", "");
    }
};


withId(R.id.my_main_editText, new Anvil.Renderable() {
    @Override
    public void view() {
        if (!btnRadioGroupValue.isEmpty()){
            if (btnRadioGroupValue.equals(valueButtonA)){
                onTextChanged(EditTextNumberSpaceTextWatcher);
                text(editTextString);
            } else if (btnRadioGroupValue.equals(valueButtonB)){
                onTextChanged(EditTextNumberNormalTextWatcher);
                text(editTextString);
            }
        }
    }
});

这是我的 EditTextNumberSpaceTextWatcher.java:

public abstract class EditTextNumberSpaceTextWatcher extends PhoneTextWatcher {
    private static final String MY_NUMBER_FORMAT = "**** **** **** **** **** **** **** ****";

    public EditTextNumberSpaceTextWatcher() {
        super(MY_NUMBER_FORMAT );
    }

    @Override
    public char[] getSeparatorCharacters() {
        return new char[]{' '};
    }
}

这是我的 EditTextNumberNormalTextWatcher.java:

public abstract class EditTextNumberNormalTextWatcherextends PhoneticTextWatcher {
    private static final String MY_NUMBER_FORMAT = "********************************";

    public EditTextNumberNormalTextWatcher() {
        super(MY_NUMBER_FORMAT );
    }
}

我认为您可以只使用一个 TextWatcher:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//here evaluate the value of radio button
 if(mRadioButton1.isChecked()){
      //set pattern
   }else if(mRadioBUtton2.isChecked()){
     //set another pattern
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM