开发者

Soft keyboard enter key event handle

I want to do some stuff on soft keyboard enter key press. See my code and output below. It executes twice, 开发者_开发百科but I want it to be executed only once. How to fix it?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    txt = (EditText)findViewById(R.id.txt);
    txt.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_ENTER) {
                Log.e("test","--------- on enter");
                return false;
            }
            return false;
        }
    });
}

Soft keyboard enter key event handle


The best way possible:

if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
    Log.d(TAG, "enter_key_called");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜