如何在TextView上监听并对android中的同一TextView进行更改

时间:2014-10-21 10:31:45

标签: java android

我尝试了以下代码,但android不允许我这样做,因为我进入了一个无限循环。

mEditText = (EditText) getActivity().findViewById(R.id.input_content);
mEditText.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) {
                   //I want to format the already existing text in s in a certain way as      the user is typing
                   mEditText.setText(s.toString() + " \n");
            }       
        });

请任何想法都会非常有帮助。感谢

2 个答案:

答案 0 :(得分:1)

3种可能的解决方案:

  1. 检查字符串中的回车状态
  2. 像这样:

    mEditText = (EditText) getActivity().findViewById(R.id.input_content);
    mEditText.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)
        {
            //I want to format the already existing text in s in a certain way as      the user is typing
            if (s.toString().charAt(s.length() - 1 ) != '\n')
            {
                mEditText.setText(s.toString() + " \n");
            }
        }
    });
    

    根据输入字符串中的内容

    ,这不是最佳解决方案
    1. 在beforeTextChanged
    2. 中执行

      像这样:

      mEditText = (EditText) getActivity().findViewById(R.id.input_content);
      mEditText.addTextChangedListener(new TextWatcher()
      {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after)
          {
              //I want to format the already existing text in s in a certain way as      the user is typing
              mEditText.setText(s.toString() + " \n");
          }
      
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {}
      
          @Override
          public void afterTextChanged(Editable s) {}
      });
      
      1. 暂时取消听众
      2. 像这样:

        final TextWatcher tw = 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)
            {
                //I want to format the already existing text in s in a certain way as      the user is typing
                mEditText.removeTextChangedListener(tw);
                mEditText.setText(s.toString() + " \n");
                mEditText.addTextChangedListener(tw);
            }
        });
        
        mEditText = (EditText) getActivity().findViewById(R.id.input_content);
        mEditText.addTextChangedListener(tw);
        

答案 1 :(得分:0)

您最有可能进入无限循环,因为您正在更改public void afterTextChanged(Editable s)中的文本,然后再次调用该方法。
最好的解决方案是改变其他方法中的文本,或者改变可编辑的焦点。有关详细信息,请参阅View.OnFocusChangeListener

以下是一个例子:

textView.setOnFocusChangeListener(new OnFocusChangeListener() {          
    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus) {
            textView.setText(s.toString() + " \n");
        }
    }
});