安卓View.OnKeyListener:点击一次,执行两次 [英] Android View.OnKeyListener: click once, execute twice

查看:1988
本文介绍了安卓View.OnKeyListener:点击一次,执行两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  公共布尔onKey()调用两次?

下面是我的code

public class TestKeyActivity extends Activity {

private int i=1;
private ScrollView sv;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    sv=(ScrollView) this.findViewById(R.id.read_scrollView);

    sv.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {

        Toast.makeText(TestKeyActivity.this, "what is wrong!!!!"+(i++), 2).show();
            return true;
            }
            return false;
        }
    });
    }
}

我用他的Andr​​oid模拟器和Eclipse,我不知道为什么,但是当我点击该键一次,code 吐司将执行两次。 是不是有什么毛病我code?

I use he Android emulator and Eclipse,I don't know why, but when I click the key once, the code of toast will execute twice. Is there something wrong with my code?

推荐答案

我只是回答了一个非常类似的问题(<一href="http://stackoverflow.com/questions/10377049/unwilling-edittext-ontouchlistener-function-call/10377470#10377470">here).问题是,你激活的 KeyEvent.ACTION_DOWN KeyEvent.ACTION_UP 。您应该只执行你的code如果 KeyEvent.getAction()== KeyEvent.ACTION_UP

I just answered a very similar question (here). The problem is that you're activating on KeyEvent.ACTION_DOWN and KeyEvent.ACTION_UP. You should only execute your code if KeyEvent.getAction() == KeyEvent.ACTION_UP

这篇关于安卓View.OnKeyListener:点击一次,执行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆