activity_main.xml
<?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:background="#E6E6E6" android:orientation="vertical" > <com.eiice.xiaokeke.SoftKeyBoardSatusView android:id="@+id/login_soft_status_view" android:layout_width="fill_parent" android:layout_height="fill_parent" > </com.example.test.SoftKeyBoardSatusView> <LinearLayout android:id="@+id/login_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/login_icon" android:layout_width="75dp" android:layout_height="75dp" android:layout_gravity="center_horizontal" android:layout_marginTop="100dp" android:src="@drawable/default_head_icon" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="5dp" android:background="@drawable/login_edit_bg" android:orientation="vertical" android:paddingTop="10dp" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/login_edit_account" android:layout_width="fill_parent" android:layout_height="45dp" android:layout_marginRight="65dp" android:background="@android:color/transparent" android:gravity="center_vertical" android:hint="请输入账号" android:paddingLeft="10dp" android:singleLine="true" android:textColor="@android:color/black" android:textSize="18sp" /> <ImageView android:id="@+id/login_arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:paddingBottom="10dp" android:paddingLeft="5dp" android:paddingRight="7dp" android:paddingTop="10dp" android:src="@drawable/arro_down" android:textSize="18sp" /> <ImageView android:id="@+id/login_account_edit_clear" android:layout_width="16dp" android:layout_height="16dp" android:layout_centerVertical="true" android:layout_marginRight="2dp" android:layout_toLeftOf="@id/login_arrow" android:src="@drawable/common_input_box_clear" /> </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:id="@+id/login_edit_password" android:layout_width="fill_parent" android:layout_height="45dp" android:layout_marginRight="65dp" android:background="@android:color/transparent" android:gravity="center_vertical" android:hint="请输入密码" android:inputType="textPassword" android:paddingLeft="10dp" android:paddingRight="45dp" android:singleLine="true" android:textColor="@android:color/black" android:textSize="18sp" /> <ImageView android:id="@+id/login_password_edit_clear" android:layout_width="16dp" android:layout_height="16dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:src="@drawable/common_input_box_clear" /> </RelativeLayout> </LinearLayout> <Button android:id="@+id/login_login_btn" android:layout_width="fill_parent" android:layout_height="45dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="40dp" android:background="@drawable/login_btn_selector" android:gravity="center" android:text="登 录" android:textColor="@android:color/white" android:textSize="22sp" android:textStyle="bold" /> <Button android:id="@+id/login_register_btn" android:layout_width="fill_parent" android:layout_height="45dp" android:layout_marginBottom="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:background="@drawable/login_btn_selector" android:gravity="center" android:text="注 册" android:textColor="@android:color/white" android:textSize="22sp" android:textStyle="bold" /> </LinearLayout> </RelativeLayout>
MainActivity.java
package com.eiice.xiaokeke; import com.example.test.R; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends Activity implements SoftKeyBoardSatusView.SoftkeyBoardListener { Button button_register; SoftKeyBoardSatusView satusView; LinearLayout login_layout; int scroll_dx; int screenHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_register = (Button) findViewById(R.id.login_register_btn); satusView = (SoftKeyBoardSatusView) findViewById(R.id.login_soft_status_view); login_layout = (LinearLayout) findViewById(R.id.login_layout); screenHeight = getWindowManager().getDefaultDisplay().getHeight(); satusView.setSoftKeyBoardListener(this); } @Override public void keyBoardStatus(int w, int h, int oldw, int oldh) { } @Override public void keyBoardVisable(int move) { int[] location = new int[2]; button_register.getLocationOnScreen(location); int btnToBottom = screenHeight - location[1] - button_register.getHeight(); scroll_dx = btnToBottom > move ? 0 : move - btnToBottom; login_layout.scrollBy(0, scroll_dx); } @Override public void keyBoardInvisable(int move) { login_layout.scrollBy(0, -scroll_dx); } }
SoftKeyBoardSatusView.java
package com.eiice.xiaokeke; import com.example.test.R; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends Activity implements SoftKeyBoardSatusView.SoftkeyBoardListener { Button button_register; SoftKeyBoardSatusView satusView; LinearLayout login_layout; int scroll_dx; int screenHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_register = (Button) findViewById(R.id.login_register_btn); satusView = (SoftKeyBoardSatusView) findViewById(R.id.login_soft_status_view); login_layout = (LinearLayout) findViewById(R.id.login_layout); screenHeight = getWindowManager().getDefaultDisplay().getHeight(); satusView.setSoftKeyBoardListener(this); } @Override public void keyBoardStatus(int w, int h, int oldw, int oldh) { } @Override public void keyBoardVisable(int move) { int[] location = new int[2]; button_register.getLocationOnScreen(location); int btnToBottom = screenHeight - location[1] - button_register.getHeight(); scroll_dx = btnToBottom > move ? 0 : move - btnToBottom; login_layout.scrollBy(0, scroll_dx); } @Override public void keyBoardInvisable(int move) { login_layout.scrollBy(0, -scroll_dx); } }
原文地址:http://download.csdn.net/download/lgl1170860350/8468565