Android View.OnKeyListener: click once, execute twice

Possible duplicate:
public boolean onKey () is called twice?

Here is my 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;
        }
    });
    }
}

I use its Android emulator and Eclipse, I don’t know why, but when I press the key once, the toast code will be executed twice. Is there something wrong with my code?

+5
source share
1 answer

I just answered a very similar question ( here ). The problem is that you activate the KeyEvent.ACTION_DOWNand buttons KeyEvent.ACTION_UP. You should execute your code only ifKeyEvent.getAction() == KeyEvent.ACTION_UP

+11
source

All Articles