Possible Duplicate:
public boolean onKey() 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 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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With