美文网首页Android UIAndroidAndroid开发者俱乐部
Android EditText悬浮在输入法之上

Android EditText悬浮在输入法之上

作者: didikee | 来源:发表于2016-08-04 14:42 被阅读2374次

Android EditText悬浮在输入法之上

使用 android:windowSoftInputMode="adjustResize" 会让界面整体被顶上去,很多时候我们不需要这样的情况出现,这里给出另一个方案.

**思路:监听输入法的状态,然后动态的滚动 EditText 所在的 ViewGroup 或者View **

1. Android Manifest.xml

<activity
        android:name=".InputActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan"> //非adjustResize
 </activity>

2. 布局文件

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_saber_q"
    tools:context="didikee.com.demoapk.InputActivity">
    <LinearLayout
        android:id="@+id/rl_inputdlg_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#00000000"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/input_message"
            android:layout_width="0dp"
            android:layout_height="@dimen/dp30"
            android:layout_gravity="center|left"
            android:layout_marginLeft="@dimen/dp12"
            android:paddingLeft="@dimen/dp2"
            android:paddingRight="@dimen/dp2"
            android:layout_weight="1"
            android:background="@drawable/shape_cricle_gray_solid_white"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="left"
            android:imeOptions="actionSend"
            android:maxLength="32"
            android:singleLine="true"
            android:text=""
            android:textColor="@color/dark_text"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/confrim_btn"
            android:layout_width="@dimen/dp50"
            android:layout_height="@dimen/dp30"
            android:layout_gravity="center|right"
            android:layout_marginLeft="@dimen/dp11"
            android:layout_marginRight="@dimen/dp12"
            android:background="@drawable/shape_cricle_solid_orange"
            android:gravity="center"
            android:text="发送"
            android:textColor="@color/white"/>
    </LinearLayout>

</RelativeLayout>

3. Activity里设置监听,滚动 input 视图

public class InputActivity extends AppCompatActivity {

    private RelativeLayout rLayout;
    private View mInputLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);
        mInputLayout = findViewById(R.id.rl_inputdlg_view);

        rLayout = ((RelativeLayout) findViewById(R.id.root));
        //输入法到底部的间距(按需求设置)
        final int paddingBottom = DisplayUtil.dp2px(this, 5);

        rLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                rLayout.getWindowVisibleDisplayFrame(r);
                //r.top 是状态栏高度
                int screenHeight = rLayout.getRootView().getHeight();
                int softHeight = screenHeight - r.bottom ;
                Log.e("test","screenHeight:"+screenHeight);
                Log.e("test","top:"+r.top);
                Log.e("test","bottom:"+r.bottom);
                Log.e("test", "Size: " + softHeight);
                if (softHeight>100){//当输入法高度大于100判定为输入法打开了
                    rLayout.scrollTo(0, softHeight+paddingBottom);
                }else {//否则判断为输入法隐藏了
                    rLayout.scrollTo(0, paddingBottom);
                }
            }
        });
    }
}

效果图:

1. 不做处理前:

2. 处理后的效果:

补充:

在我的手机上(三星s7)输入法的高度在1000px左右(我的输入法高度是1040px),理论上你只要把这个值放在 0~~1040直接就可以检测到输入法的展开和隐藏.

在代码中写的尺寸都是px单位的,100px也只有50dp不到,一般输入法的高度不可能比它小.当然你不喜欢可以设其他的,但是不宜过大.=.=

相关文章

网友评论

  • Discredited:感谢作者分享 比心
  • 5c86e01fae2c:收藏一下 可能有用
  • 佛系媛猿:为啥同样这样设置了 输入框还是一直在布局底部 没有在输入法上面 虽然整个界面没有因为输入法而被顶上去
    didikee:@Jellyguoguo 你能把代码发给我看看吗?我也很想知道你是怎样出现的
    佛系媛猿:@didikee 嗯,我按照你的方法给rootview增加了监听,实现了rootview不会被输入法顶上去,但是inputview还是看不见。被输入法遮挡了,不会在输入法上面显示
    didikee:@Jellyguoguo 是获取rootview(根布局)的吗?能具体一点描述吗?
  • 彼岸sakura:请教一下输入法高度大于100时滚动,这个100是怎么得出来的?谢了哈
    didikee:@彼岸sakura 随便设的,但是是有依的,你要是运行我的代码就会发现输入法的高度在1000px左右(我的输入法高度是1040px),理论上你只要把这个值放在 0~~1040直接就可以检测到输入法的展开和隐藏.谢谢你的回复,我去更新下blog :kissing_heart:
  • 百年叔叔:正常需要。写的好,明天再看看。尝试封装一下
    didikee:@百年叔叔 谢谢,我也是在项目需求下做的效果 :blush:

本文标题:Android EditText悬浮在输入法之上

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