2017-08-14 21:44:23

有很多中情况,分别展示。

1、Activity不做任何设置,布局使用LinearLayout

会自动滚动EditText之上的所有View,代码:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/rrr"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:text="这是标题"
android:textColor="#0f0980"/> <EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:hint="第1个"
android:textColor="#0f0980"/> <EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:hint="第2个"
android:textColor="#0f0980"/> <EditText
android:id="@+id/edit3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:hint="第3个"
android:textColor="#0f0980"/> <EditText
android:id="@+id/edit4"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:hint="第4个"
android:textColor="#0f0980"/> <EditText
android:id="@+id/edit5"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:hint="第5个"
android:textColor="#0f0980"/> <EditText
android:id="@+id/edit6"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:hint="第6个"
android:textColor="#0f0980"/> <EditText
android:id="@+id/edit7"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:hint="第7个"
android:textColor="#0f0980"/> <EditText
android:id="@+id/edit8"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:hint="第8个"
android:textColor="#0f0980"/>
</LinearLayout>

效果图:

2、Activity不做任何设置,布局使用RelativeLayout、FrameLayout

使用RelativeLayout和FrameLayout都是一样的,会自动上划至EditText完全展示。

3、关于Focus

如果要想指定某个EditText获取焦点,需要在代码中如下调用

 mEdit8.requestFocus();

还有2个关于焦点的方法,有时候需要配合使用,如下:

 mEdit1.setFocusable(false);
mEdit1.setFocusableInTouchMode(false);
mEdit1.requestFocus();

默认是页面中第一个EditText获取焦点,如果第一个EditText不想获取焦点,则系统自动选择第二个EditText获取焦点。如果都设置不获取焦点,则都没有焦点。

需要注意的是,通过setFocus***设置为不获取焦点,点击EditText时不会获取焦点,不会弹出软键盘,所以要慎用。

4、获取焦点,并展示软键盘

不能在onCreate()方法中直接调用展示软键盘的操作,可用Handler或者点击时显示软键盘,一种方案是:

    mEdit8.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(mEdit8, InputMethodManager.SHOW_FORCED);
}
}, 100);

5、EditText不获取焦点

如果想要在进入页面时,页面中所有的EditText都不获取焦点,用户点击时才获取焦点,不好的做法如下:

     <EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="460dp"
android:layout_marginTop="70dp"
android:hint="第1个"
7 android:focusable="false"
8 android:focusableInTouchMode="false"
android:textColor="#0f0980"/>

这样一来,edit1就不会获得焦点,点击也无法获得焦点。比较合理的做法是在其父ViewGroup上设置,如下:

 <FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
5 android:focusable="true"
6 android:focusableInTouchMode="true"
android:orientation="vertical">

这样一来,父View获取焦点,子View就不会再获取焦点了。同时,又不影响子View在点击时获取焦点。

6、布局最外层是ScrollView

如果布局最外层是ScrollView,那么其中的EditText会获得焦点并自动弹出软键盘,前面提到过,只会自动获取焦点,这里会自动弹出软键盘。如果不想让软件盘自动弹出来,可以让第一个EditText之前的View获取焦点即可,5中提到的父View获取焦点作用其实是一样的。

简单梳理一下:

只要页面中有EditText,在没有特殊设置的情况下,EditText会自动获得焦点。如果EditText在ScrollView中,则EditText不仅会自动获得焦点,还会弹出软键盘。要想不弹出软键盘,其实很简单,让其他优先于EditText获取到焦点的View得到焦点即可。

同时以上内容均是在没有设置任何windowSoftInputMode,使用默认值stateUnspecified。

7、stateVisible

The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window).

强制召唤软键盘,如果有获得焦点的EditText,则在EditText上弹出。

如果页面上没有EditText或者所有的EditText都不可见呢?软键盘还是会弹出来的,这种行为很怪异,需谨慎。

8、stateAlwaysVisible

The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

用户选择activity时,软键盘总是处于显示的状态,与stateVisible的区别是:stateVisible的Activity在跳转到其他Act再次返回时,软键盘不会展示,而stateAlwaysVisible则会一直处于展示状态。

9、stateUnchanged

The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore

当这个activity出现时,软键盘将一直保持在上一个activity里的状态,无论是隐藏还是显示。

如果当前Activity是第一个Activity,使用这个属性和stateUnspecified是一样的。如果上一个Activity展示软键盘,则当前Activity也会展示软键盘。

10、stateHiden

The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.

用户选择activity时,软键盘总是被隐藏。但是点击EditText时又可以弹出软键盘。比如前面提到的ScrollView自动弹出软键盘,如果使用这个属性,则不会自动弹出。

11、stateAlwaysHiden

The soft keyboard is always hidden when the activity's main window has input focus.

以为应该和stateAlwaysVisible一样的,但是测试发现,跳转到其他页面回来时,即便ScrollView页面设置的是stateHiden,软键盘也不会再次展示。所以stateAlwaysHiden的作用还要继续探究。

============================================

以上是state***系列的几个属性,主要作用是控制软键盘的展示逻辑的。

从现在开始adjust***系列属性,主要是用来设置软键盘和页面UI元素之间的显示关系。

12、adjustUnspecified

It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the current focus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area.
This is the default setting for the behavior of the main window.
默认行为,由系统自行决定隐藏还是展示。默认行为下,ScrollView布局和非ScrollView布局展示软键盘的区别在于:软键盘弹出时,ScrollView只会滚动其中的内容。而非ScrollView会滚动整个页面,包括ActionBar。

13、adjustResize

The activity's main window is always resized to make room for the soft keyboard on screen.

Activity总是调整屏幕的大小以适应软键盘。非ScrollView布局会出现Window为了显示软键盘强行调整Activity主窗口大小的情况,UI和交互会变得比较诡异。ScrollView布局只会滚动其中的内容,不包括ActionBar等。

14、adjustPan

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

adjustPan采取了和adjustResize的思路,adjustPan不再调整主窗口的大小,而是通过移动内容来保证软键盘在可见范围内。经过试验使用adjustPan时,不论是不是ScrollView布局,都是整体向上滚动。

15、adjustNothing

由于默认的是adjustUnspecific,所以总会对UI产生影响,如果不想让UI随着软键盘的弹出有任何变动,可使用adjustNothing。

比如可以在代码中动态的调整是否响应adjust操作,如下:

 int orientation = UtilHelper.getRealScreenOrientation(this);
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.e("David", "横屏了----------");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
} else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.e("David", "----------竖屏了");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}

总结:

感觉不指定adjust属性或者使用adjustUnspecified时,ScrollView布局采用adjustResize,非ScrollView布局采用adjustPan。adjustPan在两种布局下行为至少是一致的,adjustResize在非ScrollView布局下,行为是不可预测的。所以,在不确定的情况下,推荐使用默认adjust属性。

Android中软键盘展示、EditText焦点获取及windowSoftInputMode属性探究的更多相关文章

  1. Android中软键盘弹出时关于布局的问题

     当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncre ...

  2. Android中软键盘弹出时底部菜单上移问题

    当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncrea ...

  3. Android隐藏软键盘收回软键盘

    代码改变世界 Android隐藏软键盘收回软键盘 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPU ...

  4. 彻底搞定Android开发中软键盘的常见问题

    软键盘显示的原理 软件盘的本质是什么?软键盘其实是一个Dialog.        InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参 ...

  5. android开发 软键盘出现后 防止EditText控件遮挡 总体平移UI

    在EditText控件接近底部的情况下 软键盘弹出后会把获得焦点的EditText控件遮挡 无法看到输入信息  防止这种情况发生 就须要设置AndroidManifest.xml的属性 前面的xml信 ...

  6. Android WebView中软键盘会遮挡输入框相关问题

    要想实现这样的软键盘出现的时候会自己主动把输入框的布局顶上去的效果,须要设置输入法的属性,有下面两种设置方式:     一.在java代码中设置例如以下:      getWindow().setSo ...

  7. Android手动控制软键盘的开启和关闭,判断软键盘是否显示;

    工具类,拿走就能用: import android.annotation.TargetApi; import android.app.Activity; import android.content. ...

  8. Android 判断软键盘弹出并隐藏的简单完美解决方案

    最近项目中有一个编辑框,下面是个ListView.在触发编辑框弹出软键盘后,ListView还能滑动,并且ListView的item还能响应单击.这样的体验效果很不好.于是便想在滑动或单击item时判 ...

  9. android 表情,软键盘冲突解决方案(仿微博等SNS应用)

    之前总想搞一下这个模块,可是由于忙碌总是推迟,现在就把这块好好的弥补过来,下面是我实现的思路.本人才疏学浅,还望大家不要见笑. 首先我们还是先看写示例:            上面应用应该不用我过多介 ...

随机推荐

  1. IDEA修改显示星号*和热部署

    IDEA修改显示*星号: IDEA热部署: 两步: 1. 2. Ctrl+Alt+Shift+/ 打开下面界面,选Registry 对于Springboot应用,可能无法启动,在上述两步不起作用的情况 ...

  2. asp.net简繁体转换

    简繁体转换添加Microsoft.VisualBasic.dll引用 //简转繁 string str= Microsoft.VisualBasic.Strings.StrConv("民生银 ...

  3. GreenDao教程2

    总述: 所有的增删改查都需要通过greendao通过实体对象类生成的Dao来实现, 具体实现如下图 1.初始化数据库操作对象(GreenDao自动生成的操作对象) 2.通过数据库操作对象,进行增删改查 ...

  4. Eclipse中Lombok的安装和注解说明

    Lombok 可用来帮助开发人员消除 Java 的重复代码,尤其是对于简单的 Java 对象(POJO),比如说getter/setter/toString等方法的编写.它通过注解实现这一目的. 官网 ...

  5. Nodejs运行错误小结

    (迁移自旧博客2017 04 15) 在使用过程中会遇到一些问题,学习过程中不定期更新. 问题一 错误如下 **events.js:72 throw er; // Unhandled 'error' ...

  6. Bytom的链式交易和花费未确认的交易

    当我们基于比原做应用的时候,在构建交易过程中会遇到以下两种情况.多个地址向一个地址转账,还有一种就是从一个地址分批次向多个地址转账.那我们今天就来介绍一下这两种交易构建的具体流程,以及贴出具体实现的代 ...

  7. js原型和原型链理解到面向对象

    一.js中的两种对象,普通对象和函数对象 var obj1 = {}; var obj2 =new Object(); var obj3 = new obj1(); function fun1(){} ...

  8. MySQL自定义函数递归查询

    用于递归查找Id(通过parentId关联)参数为int 类型的值: CREATE DEFINER=`root`@`%` FUNCTION `getChildList`(rootId INT) RET ...

  9. php接口签名验证

    在做一些api接口设计时候会遇到设置权限问题,比如我这个接口只有指定的用户才能访问. 很多时候api接口是属于无状态的,没办法获取session,就不能够用登录的机制去验证,那么 大概的思路是在请求包 ...

  10. 【Axure RP8.1】一款专业的快速原型设计工具

    Axure RP是一款专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写.Axure RP是美国Axur ...