我有一个情况,有两个领域。Field1和field2。我想要的 当field1被改变时,要做的是空field2,反之亦然。所以只有在最后 一个字段上有内容。

field1 = (EditText)findViewById(R.id.field1);
field2 = (EditText)findViewById(R.id.field2);

field1.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      field2.setText("");
   }
  });

field2.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {}

   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
     field1.setText("");
   }
  });

它工作得很好,如果我附加addTextChangedListener到field1,但当 我对两个字段都这么做,应用程序崩溃了。显然是因为他们想要改变 无限地相互联系。一旦field1改变,此时它就会清除field2 Field2被更改,因此它将清除field1,以此类推……

谁能给点建议吗?


当前回答

editText.addTextChangedListener(new TextWatcher() 
{
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) 
            {
            }

            @Override
            public void onTextChanged(CharSequence chr, int i, int i1, int i2) 
            {
                //Check char sequence is empty or not
                if (chr.length() > 0)
                {
                    //Your Code Here
                }
            }

            @Override
            public void afterTextChanged(Editable editable) 
            {
            }
 });

其他回答

您可以添加一个检查,只在字段中的文本不为空时清除(即当长度不等于0时)。

field1.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() != 0)
        field2.setText("");
   }
  });

field2.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      if(s.length() != 0)
         field1.setText("");
   }
  });

这里是TextWatcher的文档。

另外,请尊重命名约定。

在onCreate方法中动态添加背景:

getWindow().setBackgroundDrawableResource(R.drawable.background);

也从XML中删除背景。

在将另一个EditText设置为空之前检查字符串。如果Field1为空,为什么需要再次更改为("")?所以你可以用s.l end()或任何其他解决方案检查你的字符串的大小

另一种检查字符串长度的方法是:

String sUsername = Field1.getText().toString();
if (!sUsername.matches(""))
{
// do your job
}
var filenameText = findViewById(R.id.filename) as EditText
filenameText.addTextChangedListener(object : TextWatcher {
    override fun afterTextChanged(s: Editable?) {
        filename = filenameText.text.toString()
        Log.i("FileName: ", filename)
    }
    
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
})

你用这个 我已经提供了textchangelistner的最新方法

edMsg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                
                if (i2 == 0){
                    ////Edit text blanked
                } 
                
                String msg = charSequence.toString();/// your text on changed in edit text
                
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });