Android软键盘监听器?

时间:2016-06-30 10:05:05

标签: android

enter image description here

我想听这个红色按钮,我该怎么办?

2 个答案:

答案 0 :(得分:3)

活动:

final View activityRootView = findViewById(R.id.activityRoot);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();

                    activityRootView.getWindowVisibleDisplayFrame(r);

                    int heightDiff = view.getRootView().getHeight() - (r.bottom - r.top);
                    if (heightDiff > 100) { 
                     //enter code when show keyboard
                    }else{
                     //enter code when hide keyboard
                    }
                }
            });

For Fragment:

view = inflater.inflate(R.layout.live_chat_fragment, null);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //r will be populated with the coordinates of your view that area still visible.
                view.getWindowVisibleDisplayFrame(r);

                int heightDiff = view.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 500) { // if more than 100 pixels, its probably a keyboard...
                 //enter code when show keyboard
                }else{
                 //enter code when hide keyboard
                }
            }
        });

答案 1 :(得分:0)

不帮助:

setOnEditorActionListener(new OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
            Log.i(TAG,"Enter pressed");
        }    
        return false;
    }
});