android 软键盘弹出,屏幕整体滚动到底部效果

本文介绍了一种实现软键盘弹出时页面自动上移的方法,通过使用ScrollView并监听其高度变化来调整页面布局。文章详细展示了如何设置布局文件及实现onWindowFocusChanged方法以确保良好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原理:先用ScrollView包在最外面,然后通过监视它的高度 getViewTreeObserver().addOnGlobalLayoutListener,最后调用scrollTo

主配置文件代码:

<activity
            android:name="com.sharera.capitalcircle.activity.LoginActivity"
            android:windowSoftInputMode="stateAlwaysHidden|adjustResize">    
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>       
</activity>


前台页面:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/login_rootlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#dd4443" 
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/login_log"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30dp"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/login_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:id="@+id/login_form"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/login_log"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="100dp"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/login_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                android:hint="邮箱/手机号"
                android:maxLength="20" />

            <EditText
                android:id="@+id/login_pwd"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffff"
                android:hint="请输入密码"
                android:inputType="textPassword"
                android:maxLength="20" />

            <Button
                android:id="@+id/login_login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:background="@drawable/login_button"
                android:text="登录"
                android:textColor="#ffffff"
                android:textStyle="bold" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp" >

                <CheckBox
                    android:id="@+id/login_remember_pwd"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="记住密码" />

                <CheckBox
                    android:id="@+id/login_autolgoin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="自动登录" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>
</ScrollView>


 

后台关键方法:

@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		//只绑定一次
		if (hasFocus && firstLoad) {
			firstLoad = false;
			layout = (ScrollView) findViewById(R.id.login_rootlayout);
			layout.getViewTreeObserver().addOnGlobalLayoutListener(
					new OnGlobalLayoutListener() {
						@Override
						public void onGlobalLayout() {
							//绑定后会触发一次,所以第一次不滚动。
							if (firstChange) {
								firstChange = false;
								layoutHeight=layout.getMeasuredHeight();								
							} else {
								int h=layout.getMeasuredHeight();
								if(h<layoutHeight)
								{	
									layout.scrollTo(0, layoutHeight);
								}								
							}
						}
					});
		}
	}


这个效果是一开始没有键盘显示,点击输入框才会弹出软键盘,页面整体上移,如果没有adjustResize,layout.scrollTo不会触发,如果不加stateAlwaysHidden,屏幕加载完会直接弹键盘。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值