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

来源:互联网 发布:unix网络编程卷2 编辑:程序博客网 时间:2024/05/16 11:41

前段时间有个妹子问我如题的需求,我就想,这种东西网上不是很多吗,自己试过才发现,基本都不行,各种设置配置文件的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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

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


转载:http://blog.csdn.net/zxq614/article/details/52640542?locationNum=2

阅读全文
0 0
原创粉丝点击