编辑文本的常量后缀即使在添加字符后也会存在

时间:2015-02-09 05:43:06

标签: android android-edittext

我想要这样的东西。

enter image description here

即使输入数字后,'X10 ^ 3C也应保持不变。

1 个答案:

答案 0 :(得分:1)

使用文本观察器来实现此目的

    //global variable
        Boolean isAvaialable = false; 

    // text watcher
        txtAdvertise.addTextChangedListener( new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

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

                    }

                    @Override
                    public void afterTextChanged(Editable s) {


                          if(!txtAdvertise.getText().toString().contains("X10^3c") && !isAvaialable) {
                              txtAdvertise.setText(txtAdvertise.getText() + "X10^3c");
                              isAvaialable = true;
                            }
                         else if(txtAdvertise.getText().length() == 0 && isAvaialable && !txtAdvertise.getText().toString().contains("X10^3c")){
                              txtAdvertise.setText(txtAdvertise.getText() + "X10^3c");
                              isAvaialable = false;
                        }
  }
        });