TextEdit值更改时如何触发?

时间:2011-09-23 12:13:12

标签: android android-edittext textwatcher

我试过了:

houseField.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            Toast.makeText( getApplicationContext(), s, 15000).show();
        }
    });

但是在插入每个符号后触发了事件,但我不想要那个..

当用户离开textedit并且文本被更改时,我需要触发事件,因为我将查询google maps api。输入每个字符后我无法进行查询。

2 个答案:

答案 0 :(得分:3)

您是否尝试过onFocusChanged()

 yourTextView.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
      if(hasFocus)
      {
        //do your logic here
      }
    }
 }

另一个选项是使用onTextChanged启动一个计时器,该计时器检查延迟进程并在另一个键击发生时重置。这种方法可能更适合您的应用,但实施起来比较棘手

修改

或者,您可以覆盖boolean View.onKeyPreIme(int keyCode, KeyEvent event)并捕获回车键(但您还需要考虑用户点击后退键以关闭键盘)。

答案 1 :(得分:0)

use 
String sChange ="";
boolean change=false;
    @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
             String new=s.toString();
             if(!sChange.equalsIgnoreCase(new))
            {
                Toast.makeText( getApplicationContext(), sChange, 15000).show();
                change=true;
            }
            else
             change=false;
           }

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

yourTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
  if(hasChanged)
  {
if(change)
//google logic

  }
}

}