如何创建Android中键盘监听器? [英] How can i create a keyboard listener in Android?

查看:153
本文介绍了如何创建Android中键盘监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要当键盘出现。如何我可以创建一个侦听器为更改特定布局的知名度?

I want to change the visibility of a particular layout when keyboard appears .How can i create a listener for that?

推荐答案

请软键盘检测类

public class RelativeLayoutThatDetectsSoftKeyboard extends RelativeLayout {

    public RelativeLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
    }

    public interface Listener
    {
        public void onSoftKeyboardShown(boolean isShowing);
    }

    private Listener listener;

    public void setListener(Listener listener) 
    {
        this.listener = listener;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    {
        int height = MeasureSpec.getSize(heightMeasureSpec);
        Activity activity = (Activity)getContext();
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int statusBarHeight = rect.top;
        int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
        int diff = (screenHeight - statusBarHeight) - height;

        if (listener != null) 
        {
            listener.onSoftKeyboardShown(diff>128); // assume all soft keyboards are at least 128 pixels high
        }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);       
    }
}

与RelativeLayoutThatDetectsSoftKeyboard类扩展您的活动,并在您的活动下面写code

Extend your activity with RelativeLayoutThatDetectsSoftKeyboard class and write below code in your activity

//要滚动布局时,键盘是可见的。

// To scroll layout when keyboard is visible

RelativeLayoutThatDetectsSoftKeyboard mChkKeyBoradVisibility;
            mChkKeyBoradVisibility = (RelativeLayoutThatDetectsSoftKeyboard)    mTabLogin_Layout.findViewById(your layout id);
            mChkKeyBoradVisibility.setListener(this);

这篇关于如何创建Android中键盘监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆