android 软键盘设置

这个是经常需要用到的 设置直接拿来用就好了

1、软键盘遮挡部分控件

android:windowSoftInputMode=”stateVisible |adjustResize” 整体上移

android:windowSoftInputMode=”adjustPan” 键盘会覆盖屏幕

2、不自动弹出键盘:
带有EditText控件的在第一次显示的时候会自动获得focus,并弹出键盘,如果不想自动弹出键盘,有两种方法:
方法一:在mainfest文件中把对应的activity设置
android:windowSoftInputMode=”stateHidden” 或者android:windowSoftInputMode=”stateUnchanged”。

  1. 动态显示 隐藏软键盘

    隐藏软键盘情况是,比如你点击了EditText 软键盘弹出,又点击别的,控件但是希望软键盘消失
    ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

显示软键盘,
((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(控件ID, 0);

基本用法属性如下

软键盘的状态

1.stateUnspecified:不显式指定软键盘的状态(隐藏或显示),状态将依赖于主题的设置或者将由系统将选择一个合适的状态;是软键盘状态的默认设置;
2.stateUnchanged: Activity获得焦点(windowsFocus?)时,软键盘保持前面的状态,无论是隐藏还是显示;
3.stateHidden:正常情况下Activity获得焦点时,软键盘是隐藏状态(直到用户自己touch文本框);–窗口打开时就会默认显示软盘4.stateAlwaysHidden : 当该主窗口获得焦点时,软键盘总是被隐藏;
5.stateVisible :正常情况下Activity获得焦点时,软键盘是显示状态(如果该窗口需要的话(即有EditText等的控件)
6.stateAlwaysVisible: 该主窗体获得焦点时。软键盘总是显示状态;

窗口的状态

1.adjustUnspecified: 默认设置,不指定主窗口是否调整以适合软键盘输入,由系统决定干什么;
2.adjustResize: 调整主窗口,也就是挤压窗口界面,将窗口调整到软键盘之上;

网上测试的

public class ResizeLayout extends LinearLayout{   
    private static int count = 0;   

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

    @Override   
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {       
        super.onSizeChanged(w, h, oldw, oldh);   

        Log.e("onSizeChanged " + count++, "=>onResize called! w="+w + ",h="+h+",oldw="+oldw+",oldh="+oldh);   
    }   

    @Override   
    protected void onLayout(boolean changed, int l, int t, int r, int b) {   
        super.onLayout(changed, l, t, r, b);   
        Log.e("onLayout " + count++, "=>OnLayout called! l=" + l + ", t=" + t + ",r=" + r + ",b="+b);   
    }   

    @Override   
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);   

        Log.e("onMeasure " + count++, "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec);   
    }
<com.winuxxan.inputMethodTest.ResizeLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"   
    android:id="@+id/root_layout"   
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    android:orientation="vertical"   
    >   

    <EditText   
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content"    
    />   
</com.winuxxan.inputMethodTest.ResizeLayout>  

运行程序,点击文本框,查看调试信息:
E/onMeasure 6(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742024
E/onMeasure 7(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742025
E/onSizeChanged 8(7960): =>onSizeChanged called! w=320,h=201,oldw=320,oldh=377
E/onLayout 9(7960): =>OnLayout called! l=0, t=0,r=320,b=201

根布局调用了onMeasure,onSizeChanged和onLayou,改变了原布局的大小

3.adjustPan : 不调整窗口,显示到状态上就是软键盘会覆盖主窗口的下半部分

测试后,系统也重新进行了measrue和layout,但是我们发现,layout过程中onSizeChanged并没有调用,这说明输入法弹出前后并没有改变原有布局的大小。

对于非 Activity的窗口,可以通过setSofeInputMode()来设置上述属性

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值