仿qq登录界面软键盘弹出时不覆盖登录注册按钮(获取软键盘高度)

来源:互联网 发布:讷河监狱事件 知乎 编辑:程序博客网 时间:2024/04/27 21:43

参考:http://www.apkbus.com/android-177754-1-1.html

用到了里面的代码和资源


基本思路是:

1、将activity的软件盘弹出模式设置为android:windowSoftInputMode="adjustResize"

2、使用一个可以重叠的布局比如FrameLayout、RelativeLayout等,在布局底部放置一个充满屏幕的自定义布局,重写onSizeChanged方法,当高度变化超过100(认为超过100时是软件盘弹出或者隐藏的操作)时,调用接口回调方法,可以认为这个变化的高度就是软键盘的高度

3、计算你想要不被隐藏的按钮距离屏幕底端的位置,如果需要移动的话,调用view的scrollBy方法移动相应的距离


SoftKeyBoardSatusView.java,这个类用于判断软键盘是否弹出

public class SoftKeyBoardSatusView extends LinearLayout {private final int CHANGE_SIZE = 100;public SoftKeyBoardSatusView(Context context, AttributeSet attrs) {super(context, attrs);init();}public SoftKeyBoardSatusView(Context context) {super(context);init();}private void init() {}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {// TODO Auto-generated method stubsuper.onSizeChanged(w, h, oldw, oldh);Log.i("demo", "w :" + w);Log.i("demo", "h :" + h);Log.i("demo", "oldw :" + oldw);Log.i("demo", "oldh :" + oldh);if (oldw == 0 || oldh == 0)return;if (boardListener != null) {boardListener.keyBoardStatus(w, h, oldw, oldh);if (oldw != 0 && h - oldh < -CHANGE_SIZE) {boardListener.keyBoardVisable(Math.abs(h - oldh));}if (oldw != 0 && h - oldh > CHANGE_SIZE) {boardListener.keyBoardInvisable(Math.abs(h - oldh));}}}public interface SoftkeyBoardListener {public void keyBoardStatus(int w, int h, int oldw, int oldh);public void keyBoardVisable(int move);public void keyBoardInvisable(int move);}SoftkeyBoardListener boardListener;public void setSoftKeyBoardListener(SoftkeyBoardListener boardListener) {this.boardListener = boardListener;}}

布局文件类似这样,主要就是放置一个SoftKeyBoardSatusView 充满布局,其余的一样:

<?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"    android:orientation="vertical"    android:background="#E6E6E6"     >         <com.example.test.SoftKeyBoardSatusView         android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:id="@+id/login_soft_status_view"        >            </com.example.test.SoftKeyBoardSatusView>        <!-- <ScrollView         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:scrollbars="none"        android:id="@+id/login_scroller"        > -->        <LinearLayout         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        android:id="@+id/login_layout"        >        <ImageView            android:layout_width="75dp"            android:layout_height="75dp"            android:src="@drawable/default_head_icon"            android:layout_gravity="center_horizontal"            android:id="@+id/login_icon"            android:layout_marginTop="100dp"            />            <LinearLayout             android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:background="@drawable/login_edit_bg"            android:layout_marginLeft="20dp"            android:layout_marginRight="20dp"            android:layout_marginTop="5dp"            android:paddingTop="10dp"            android:orientation="vertical"            >        <RelativeLayout             android:layout_width="fill_parent"            android:layout_height="wrap_content"            >            <EditText                 android:layout_width="fill_parent"                android:layout_height="45dp"                android:background="@android:color/transparent"                android:hint="请输入账号"                android:paddingLeft="10dp"                android:layout_marginRight="65dp"                android:textColor="@android:color/black"                android:gravity="center_vertical"                android:singleLine="true"                android:id="@+id/login_edit_account"                android:textSize="18sp"                />            <ImageView                 android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/arro_down"                android:layout_alignParentRight="true"                android:layout_centerVertical="true"                android:paddingRight="7dp"                android:paddingTop="10dp"                android:paddingBottom="10dp"                android:paddingLeft="5dp"                android:id="@+id/login_arrow"                android:textSize="18sp"                />            <ImageView                 android:layout_width="16dp"                android:layout_height="16dp"                android:src="@drawable/common_input_box_clear"                android:layout_centerVertical="true"                android:layout_toLeftOf="@id/login_arrow"                android:id="@+id/login_account_edit_clear"                android:layout_marginRight="2dp"                />        </RelativeLayout>                <View             android:layout_width="fill_parent"            android:layout_height="1px"            android:background="@android:color/darker_gray"            />                <RelativeLayout             android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:orientation="horizontal"            >            <EditText                 android:layout_width="fill_parent"                android:layout_height="45dp"                android:background="@android:color/transparent"                android:hint="请输入密码"                android:textColor="@android:color/black"                android:paddingLeft="10dp"                android:paddingRight="45dp"                android:singleLine="true"                android:id="@+id/login_edit_password"                android:inputType="textPassword"                android:layout_marginRight="65dp"                android:textSize="18sp"                android:gravity="center_vertical"                />            <ImageView                 android:layout_width="16dp"                android:layout_height="16dp"                android:src="@drawable/common_input_box_clear"                android:layout_centerVertical="true"                android:layout_alignParentRight="true"                android:layout_marginRight="5dp"                android:id="@+id/login_password_edit_clear"                />        </RelativeLayout>    </LinearLayout>        <Button         android:layout_width="fill_parent"        android:layout_height="45dp"        android:background="@drawable/login_btn_selector"        android:layout_marginTop="40dp"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:text="登 录"        android:textColor="@android:color/white"        android:textSize="22sp"        android:textStyle="bold"        android:id="@+id/login_login_btn"        android:gravity="center"        />    <Button         android:layout_width="fill_parent"        android:layout_height="45dp"        android:background="@drawable/login_btn_selector"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:layout_marginTop="20dp"        android:layout_marginBottom="20dp"        android:text="注 册"        android:textColor="@android:color/white"        android:textSize="22sp"        android:textStyle="bold"        android:id="@+id/login_register_btn"        android:gravity="center"        />    </LinearLayout>    <!-- </ScrollView> -->    </RelativeLayout>
然后就可以计算注册按钮button被覆盖的高度,让外层布局LinearLayout scrollBy这段距离。计算代码:

int scroll_dx;int height = getWindowManager().getDefaultDisplay().getHeight();int bottom = height - location[1] - button_register.getHeight();scroll_dx = bottom > move ? 0 : move - bottom;layout.scrollBy(0, scroll_dx);

当软件盘隐藏的时候,相应的移动回来就可以了。

简单效果图:

登录              登录2

整理后的代码下载




0 0
原创粉丝点击