我想要这样的东西。
即使输入了数字,“x10^3c”也应保持不变。

最佳答案

使用文本观察程序来实现这一点

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

07-24 09:44