开发者

How to trigger when TextEdit value changed?

I tried this:

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

But event triggered after each symbol inserted but I dont want that..

I need to trigger event when user leaves textedit and text is changed, beco开发者_JAVA技巧use i will do query to google maps api. I can't do query after each character entered.


Have you tried onFocusChanged()

 yourTextView.setOnFocusChangeListener(new OnFocusChangeListener() {

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

The other option is to use onTextChanged to start a timer which checks to delay the process and reset if another key stroke happens. This approach may be more suitable to your application but is trickier to implement

EDIT

Alternatively you can override boolean View.onKeyPreIme(int keyCode, KeyEvent event) and capture the enter key ( but you will also need to think about the user tapping the back key to dismiss keyboard).


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

  }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜