android实现软键盘弹出,editText随键盘上移,背景不动

android实现软键盘弹出,editText随键盘上移,背景不动

前段时间有个妹子问我如题的需求,我就想,这种东西网上不是很多吗,自己试过才发现,基本都不行,各种设置配置文件的windowSoftInputMode,要么背景被压缩,要么背景向上移动,要么背景不动,但是editText没有跟着动,只能自己潜心研究,找到一种方案,虽然不完美,但是基本能满足大部分人的这类需求。

老规矩,先看效果,再贴代码:
键盘隐藏
这里写图片描述
键盘弹出
这里写图片描述
下面是代码部分:

public class EditMoveActivity extends Activity {
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_move);
        imageView = (ImageView) findViewById(R.id.imageView);

        Rect outRect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        params.height = outRect.bottom - outRect.top;
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/bg"/>
        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@+id/viewEdit"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="#88f1abcd">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zhangxq.editmove">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".EditMoveActivity"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

可以看到代码很简单,需要注意的只有这么几点:

  1. 看第18行,效果基本上是依靠布局文件来实现的,布局文件最外层需要是RelativeLayout,(FrameLayout应该也行,我没有试过)。
  2. 看第22行,不能移动的部分(这里使用ImageView举例)需要放在一个LinearLayout或者RelativeLayout里,并且外层需要套一个ScrollView,少一层都不行。
  3. 看第10行,需要在代码中动态设置不能移动部分的高度(这里用ImageView举例)。
  4. 看第69行,配置文件需要设置:android:windowSoftInputMode=“adjustResize”
  5. 至于我说的不完美的地方是:如果你需要通过setFlag的方式隐藏状态栏,那么背景还是会上移。
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值