TextEdit值改变时如何触发?

发布于 2024-12-06 09:57:44 字数 760 浏览 3 评论 0原文

我尝试了这个:

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

但是插入每个符号后都会触发事件,但我不希望这样。

当用户离开文本编辑并且文本发生更改时,我需要触发事件,因为我将对谷歌地图API进行查询。输入每个字符后我无法进行查询。

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, becouse i will do query to google maps api. I can't do query after each character entered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2024-12-13 09:57:44

您是否尝试过 onFocusChanged()

 yourTextView.setOnFocusChangeListener(new OnFocusChangeListener() {

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

另一种选择是使用 onTextChanged 启动计时器,该计时器会检查延迟过程并在发生另一次击键时重置。这种方法可能更适合您的应用程序,但实现起来比较困难

EDIT

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

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).

深海少女心 2024-12-13 09:57:44
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

  }
}

}

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

  }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文