手机软键盘监测

EditText etSearch = findViewById(R.id.etSearch);
SoftKeyBoardListener.setListener(mActivity, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                Toast.makeText(mContext, "显示"+height, Toast.LENGTH_SHORT).show();
                //软键盘显示的监测
            }

            @Override
            public void keyBoardHide(int height) {
                Toast.makeText(mContext, "隐藏"+height, Toast.LENGTH_SHORT).show();
                //软键盘隐藏的监测
            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                int action = keyEvent.getAction();
                int keyCode = keyEvent.getKeyCode();
                if ((action == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode ==                         KeyEvent.KEYCODE_ENTER)) {
                    //监测软键盘 按钮按下
                    hideSoftInput();
                    return true;
                }
                return false;
            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //输入框文本变化前调用
            } 
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //输入框文本变化时调用
            }
            @Override
            public void afterTextChanged(Editable editable) {
                //输入框文本变化后调用  这个用来监测变化后的操作
            }
        });

/** SoftKeyBoardListener.java **/

package com.shangame.fiction.core.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;

/**
 * @describe: 监听软件盘
 * @author: berlin
 * @time: 2020/05/23
 * @other:
 */
public class SoftKeyBoardListener {
    private View rootView;//activity的根视图
    int rootViewVisibleHeight;//纪录根视图的显示高度
    private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;

    public SoftKeyBoardListener(Activity activity) {
        //获取activity的根视图
        rootView = activity.getWindow().getDecorView();

        //监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //获取当前根视图在屏幕上显示的大小
                Rect r = new Rect();
                rootView.getWindowVisibleDisplayFrame(r);

                int visibleHeight = r.height();
                System.out.println(""+visibleHeight);
                if (rootViewVisibleHeight == 0) {
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
                if (rootViewVisibleHeight == visibleHeight) {
                    return;
                }

                //根视图显示高度变小超过200,可以看作软键盘显示了
                if (rootViewVisibleHeight - visibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

                //根视图显示高度变大超过200,可以看作软键盘隐藏了
                if (visibleHeight - rootViewVisibleHeight > 200) {
                    if (onSoftKeyBoardChangeListener != null) {
                        onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
                    }
                    rootViewVisibleHeight = visibleHeight;
                    return;
                }

            }
        });
    }

    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardShow(int height);

        void keyBoardHide(int height);
    }

    public static void setListener(Activity activity, OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
        SoftKeyBoardListener softKeyBoardListener = new SoftKeyBoardListener(activity);
        softKeyBoardListener.setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
    }

    /**
     * Show the soft input.
     *
     * @param activity The activity.
     */
    public static void showSoftInput(final Activity activity) {
        InputMethodManager imm =
                (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        View view = activity.getCurrentFocus();
        if (view == null) {
            view = new View(activity);
            view.setFocusable(true);
            view.setFocusableInTouchMode(true);
            view.requestFocus();
        }
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    /**
     * Show the soft input.
     *
     * @param view The view.
     */
    public static void showSoftInput(final View view) {
        InputMethodManager imm =
                (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    }

    /**
     * Hide the soft input.
     *
     * @param activity The activity.
     */
    public static void hideSoftInput(final Activity activity) {
        InputMethodManager imm =
                (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        View view = activity.getCurrentFocus();
        if (view == null) view = new View(activity);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    /**
     * Hide the soft input.
     *
     * @param view The view.
     */
    public static void hideSoftInput(final View view) {
        InputMethodManager imm =
                (InputMethodManager) Utils.getApp().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm == null) return;
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<reg51.h> #include<string.h> #define uchar unsigned char #define uint unsigned int #define delaynop() {_nop_();_nop_();_nop_();_nop_();} sbit RS=P3^5; sbit RW=P3^6; sbit EN=P3^7; sbit spk=P2^7; uchar code keypad_chars[12][6]={"1 ","2abc","3def","4ghi","5jkl","6mno","7pqrs","8tuv","9wxyz","*","0+","#"}; uchar code line1[]=" mobile keypad "; uchar code line2[]="key_char: "; uchar keyno=-1; uchar inner_idx=0; uchar tspan=0; void delayms(uchar i) { uchar x; while(i--) { for(x=0;x<120;x++); } } //uchar get_lcd_status() uchar busy_waiting() { uchar lcd_status; RS=0; RW=1; EN=1; delayms(1); lcd_status=P0; EN=0; return lcd_status; } void write_lcd_data(uchar dat) { while((busy_waiting()&&0x80)==0x80); RS=1;RW=0;EN=0;P0=dat;EN=1;delayms(1);EN=0; } void write_lcd_command(uchar cmd) { while((busy_waiting()&0x80)==0x80); RS=0; RW=0; EN=0; P0=cmd;EN=1;delayms(1);EN=0; } void init_lcd() { write_lcd_command(0x38); delayms(1); write_lcd_command(0x01); delayms(1); write_lcd_command(0x06); delayms(1); write_lcd_command(0x0c); delayms(1); } void showstring(uchar x,uchar y,uchar *str) { uchar i; if(y==0)write_lcd_command(0x80|x); if(y==1)write_lcd_command(0xc0|x); for(i=0;i<16;i++) { write_lcd_data(str[i]); } } void set_lcd_pos(uchar position) { write_lcd_command(position|0x80); } void key_scan() { P1=0x0f;delayms(1); switch(P1) { case 0x0e:keyno=0;break; case 0x0d:keyno=1;break; case 0x0b:keyno=2;break; } P1=0xf0;delayms(1); switch(P1) { case 0xe0:keyno+=0;break; case 0xd0:keyno+=3;break; case 0xb0:keyno+=6;break; case 0x70:keyno+=9; } } void beep() { uchar i; for(i=0;i<60;i++) { spk=!spk;delayms(1); } spk=1; delayms(100); } void to_int() interrupt 1 { TH0=-50000/256; TL0=-50000%256; if(tspan<50)tspan++;else TR0=0; } void main() { uchar i,pre_keyno=-1; IE=0x82; TMOD=0x01; TH0=-50000/256; TL0=-50000%256; P0=P2=0xff; delayms(10); init_lcd(); set_lcd_pos(0); for(i=0;i<16;i++) write_lcd_data(line1[i]); set_lcd_pos(0x40); for(i=0;i<16;i++) write_lcd_data(line2[i]); while(1) { P1=0xf0; if(P1!=0xf0)key_scan();else continue; if(pre_keyno!=keyno) { pre_keyno=keyno; tspan=0; inner_idx=0; TR0=1; } else { if(tspan<=40) inner_idx=(inner_idx+1)%strlen(keypad_chars[keyno]); else inner_idx=0; tspan=0; } set_lcd_pos(0x4b); write_lcd_data(keypad_chars[keyno][inner_idx]); beep(); delayms(400); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值