软键盘出现时如何捕捉事件

时间:2012-09-24 17:57:59

标签: android android-widget

有没有办法赶上活动?

我的意思是当我点击编辑框内部然后会出现软键盘。

出现带有回调功能的软键盘。有可能吗?

1 个答案:

答案 0 :(得分:3)

//Clicking on the text box     

   edittext.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(
                INPUT_METHOD_SERVICE);
        imm.showSoftInput(getCurrentFocus().getWindowToken(), //some flag here);
        }

   });

     //Being inside the box and pressing a key
     edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            //If the event is a key-down event on the "enter" button
            //If enter is pressed while inside the textbox
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {

                    InputMethodManager imm = (InputMethodManager) getSystemService(
                        INPUT_METHOD_SERVICE);
                    //Example of hiding keyboard inside enter pressed check
                    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

........