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. 处理后的效果:

Android EditText悬浮在输入法之上的更多相关文章

  1. 89、Android EditText 悬浮停靠

    package com.willen.topFloatDemo; import android.content.Context; import android.os.Handler; import a ...

  2. Android Edittext聚焦时输入法挡住了EditText输入框的两种解决方案

    方案一.把整个布局文件用ScrollView套住.这样当你聚焦时虽然输入法也能够挡住一些输入框,但是你可以通过手动滑动看被挡住的内容. 方案二.在Activity中设置android:windowSo ...

  3. Android EditText 不弹出输入法

    当第一次进入一个activity的时候  一般是第一个edittext是默认选中的,但是该死的软键盘也一起弹出来了 那是相当的不美观哈!(#‵′)凸.为此, 本大人就去寻找在刚进入这个activity ...

  4. Android EditText不弹出输入法焦点问题的总结

    转自:http://mobile.51cto.com/aprogram-403138.htm 看一个manifest中Activity的配置,如果这个页面有EditText,并且我们想要进入这个页面的 ...

  5. Android EditText自动弹出输入法焦点

    http://mobile.51cto.com/aprogram-403138.htm 1. 看一个manifest中Activity的配置,如果这个页面有EditText,并且我们想要进入这个页面的 ...

  6. Android EditText默认不弹出输入法,以及获取光标,修改输入法Enter键的方法

    一.Android EditText默认不弹出输入法的办法:1. 在AndroidManifest.xml中将需要默认隐藏键盘的Activity中添加属性即可(常用此方法) android:windo ...

  7. Android EditText不弹出输入法总结,焦点问题的总结

    看一个manifest中Activity的配置,如果这个页面有EditText,并且我们想要进入这个页面的时候默认弹出输入法,可以这样设置这个属相:android:windowSoftInputMod ...

  8. Android WindowManager悬浮窗:不需要申请权限实现悬浮

     Android WindowManager悬浮窗:不需要申请权限实现悬浮 附录文章1介绍了Android平台上的悬浮窗WindowManager,WindowManager悬浮窗可以悬浮在And ...

  9. Android EditText属性

    1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...

随机推荐

  1. tomcat的安全配置(禁用http方法,部署多个应用,启用从安全cookie,指定错误页面和显示信息)

    配置版本:tomcat6 1,虚拟路径,可以配置多个host在一个tomcat中,docbase是web应用目录,此处在server.xml中添加应用配置,要让server.xml配置生效需要重启to ...

  2. 让mysql不能为空的字段为空时也能插入

    第一步: 在mysql安装目录中找到my.ini将: #sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTIT ...

  3. 网站banner写法

    css .banner{ width: %; height: 375px; background: url(X.jpg) no-repeat center;} html <div class=& ...

  4. [翻译] ORMLite document -- How to Use Part (一)

    前言 此文档翻译于第一次学习 ORMLite 框架,如果发现当中有什么不对的地方,请指正.若翻译与原文档出现任何的不相符,请以原文档为准.原则上建议学习原英文文档. ----------------- ...

  5. Code First 关系配置整理

    之前EF一直有性能问题以及使用便利性问题, 终于到了EF6有了Migrations之后, 小弟也决定加入EF阵营了. 在学习FluentAPI配置关系的时候, 发现网上的好几个教程实际上博主自己都没有 ...

  6. XVI Open Cup named after E.V. Pankratiev. GP of Ukraine

    A. Associated Vertices 首先求出SCC然后缩点,第一次求出每个点能到的点集,第二次收集这些点集即可,用bitset加速,时间复杂度$O(\frac{nm}{64})$. #inc ...

  7. Shell运算符:Shell算数运算符、关系运算符、布尔运算符、字符串运算符等

    摘自:http://c.biancheng.net/cpp/view/2736.html

  8. [转] 前后端分离开发模式的 mock 平台预研

    引入 mock(模拟): 是在项目测试中,对项目外部或不容易获取的对象/接口,用一个虚拟的对象/接口来模拟,以便测试. 背景 前后端分离 前后端仅仅通过异步接口(AJAX/JSONP)来编程 前后端都 ...

  9. HTML解析器HtmlAgilityPack的一些使用总结(C#)

    哎~本来这些总结是作为使用时的快速备注,但是用不上了.实际应用当中HtmlAgilityPack的可靠性不太稳定,一主要问题是:-> 一些字符会出现乱码或者变成'?',如韩语字符.由于我是已经有 ...

  10. C#输出文本树形层次,前或者后自定义空格位数

    Indent String with Spaces This example shows how to indent strings using method for padding in C#. T ...