美文网首页
android 设置键盘按钮为发送按钮并监听 及 键盘显示与隐藏

android 设置键盘按钮为发送按钮并监听 及 键盘显示与隐藏

作者: tigg | 来源:发表于2017-07-26 17:17 被阅读367次
    <EditText
                android:id="@+id/message_input"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:imeOptions="actionSend"
                android:inputType="text"
                android:maxLines="1"
                android:singleLine="true" />
    

    android:imeOptions="actionSend" // 起关键作用

     messageInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEND) {
                        sendMessage(Body_type.txt, "");
                        return true;
                    }
                    return false;
                }
            });
    

    键盘显示与隐藏 监听

    public class KeyboardStatusDetector {
    
        KeyboardVisibilityListener visibilityListener;
    
        boolean keyboardVisible = false;
    
        public void registerFragment(Fragment f) {
            registerView(f.getView());
        }
    
        public KeyboardStatusDetector registerActivity(Activity a) {
            return registerView(a.getWindow().getDecorView().findViewById(android.R.id.content));
        }
    
        public KeyboardStatusDetector registerView(final View v) {
            v.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    v.getWindowVisibleDisplayFrame(r);
    
                    int heightDiff = v.getRootView().getHeight() - (r.bottom - r.top);
                    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                        /** Check this variable to debounce layout events */
                        if (!keyboardVisible) {
                            keyboardVisible = true;
                            if (visibilityListener != null)
                                visibilityListener.onVisibilityChanged(true);
                        }
                    } else {
                        if (keyboardVisible) {
                            keyboardVisible = false;
                            if (visibilityListener != null)
                                visibilityListener.onVisibilityChanged(false);
                        }
                    }
                }
            });
    
            return this;
        }
    
        public KeyboardStatusDetector setVisibilityListener(KeyboardVisibilityListener listener) {
            visibilityListener = listener;
            return this;
        }
    
        public static interface KeyboardVisibilityListener {
            public void onVisibilityChanged(boolean keyboardVisible);
        }
    }
    
    

    // 使用:

     View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
    
            new KeyboardStatusDetector()
                    .registerView(rootView)  //or register to an activity
                    .setVisibilityListener(new KeyboardStatusDetector.KeyboardVisibilityListener() {
                        @Override
                        public void onVisibilityChanged(boolean keyboardVisible) {
                            if (keyboardVisible) {   // 显示键盘
                               
                            }
                        }
                    });
    

    相关文章

      网友评论

          本文标题:android 设置键盘按钮为发送按钮并监听 及 键盘显示与隐藏

          本文链接:https://www.haomeiwen.com/subject/mrsokxtx.html