主要原理:監聽軟鍵盤的彈出,調用ScrollView的
fullScroll(ScrollView.FOCUS_DOWN);或者是scrollTo(0, 1000)的方法
已如下xml布局為例:
總得來說就是將需要向上頂的布局寫在ScrollView中。理解后可略過。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_contain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:fitsSystemWindows="true">
<include
android:id="@+id/title"
layout="@layout/include_login_header" />
<ScrollView
android:id="@+id/sv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/title">
<RelativeLayout
android:id="@+id/rl_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_login_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:src="@mipmap/logo_login" />
<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/iv_login_logo"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="58dp"
android:background="@null"
android:hint="@string/login_username"
android:inputType="phone"
android:paddingBottom="12dp"
android:singleLine="true"
android:textColorHint="@color/search_edittext_hint"
android:textSize="@dimen/font_size_14" />
<View
android:id="@+id/v_line1"
style="@style/horizontal_line_gray"
android:layout_below="@+id/et_username"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp" />
<EditText
android:id="@+id/et_passwork"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/v_line1"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="16dp"
android:background="@null"
android:hint="@string/login_password"
android:inputType="textPassword"
android:paddingBottom="12dp"
android:singleLine="true"
android:textColorHint="@color/search_edittext_hint"
android:textSize="@dimen/font_size_14" />
<View
android:id="@+id/v_line2"
style="@style/horizontal_line_gray"
android:layout_below="@+id/et_passwork"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp" />
<RelativeLayout
android:id="@+id/rl_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/v_line2">
<TextView
android:id="@+id/tv_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:padding="8dp"
android:text="@string/login_register"
android:textColor="@color/search_edittext_hint" />
<TextView
android:id="@+id/tv_login_forget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:padding="8dp"
android:text="@string/login_forget"
android:textColor="@color/search_edittext_hint" />
</RelativeLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true"
android:layout_below="@+id/rl_1"
android:layout_centerHorizontal="true"
android:layout_marginLeft="38dp"
android:layout_marginRight="38dp"
android:layout_marginTop="22dp"
android:background="@drawable/btn_normal_bg"
android:gravity="center"
android:text="@string/login_btn"
android:textColor="@color/white"
android:textSize="@dimen/font_size_15" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
首先提供一個軟鍵盤監聽的工具類
package com.xinsundoc.doctor.utils;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ScrollView;
/**
* 類名稱:KeyboardUtil<br>
* 內容摘要: 監聽軟鍵盤顯示與隱藏
*/
public class KeyboardUtil {
private static final String TAG = "KeyboardUtil";
public static KeyboardUtil assistActivity(Activity activity, int viewId) {
return new KeyboardUtil(activity, viewId);
}
private View mChildOfContent;
private ScrollView mScrollView; //從activity傳進來的ScrollView
private KeyboardUtil(Activity activity, int viewId) {
FrameLayout content = (FrameLayout) activity
.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mScrollView = (ScrollView) content.findViewById(viewId); //從activity 傳進來的ScrollView
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
possiblyResizeChildOfContent();
}
});
}
private void possiblyResizeChildOfContent() {
int contentHeight = mChildOfContent.getRootView().getHeight();
int curDisplayHeight = computeUsableHeight();
if (contentHeight - curDisplayHeight > contentHeight / 4) { //軟鍵盤彈出
Log.e(TAG, "possiblyResizeChildOfContent: 軟鍵盤彈出" );
// mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
//將ScrollView滑動到底部
//這里不開線程的話,會出現點擊第二個輸入框不頂上去的bug,
//初步估計是mScrollView.scrollTo(0, 1000);的原因(最好使用handler)
new Thread(){
@Override
public void run() {
mScrollView.scrollBy(0, 1000);
}
}.start();
if(mKeyBoardOpenListener != null){
mKeyBoardOpenListener.open();
}
} else { //軟鍵盤關閉
if(mKeyBoardOpenListener != null){
mKeyBoardOpenListener.close();
}
Log.e(TAG, "possiblyResizeChildOfContent: 軟鍵盤關閉" );
}
}
/**
* 獲取屏幕可顯示區域高度
*
* @return
*/
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return r.height();
}
private KeyBoardOpenListener mKeyBoardOpenListener;
/**
* 給其他類提供軟鍵盤彈起關閉的監聽
*
*/
public interface KeyBoardOpenListener{
void open();
void close();
}
public void setOnKeyBoardOpenListener(KeyBoardOpenListener mKeyBoardOpenListener){
this.mKeyBoardOpenListener = mKeyBoardOpenListener;
}
}
方法一:使用fullScroll(ScrollView.FOCUS_DOWN):
使用者中方法有一個問題:
如果一個ScrollView中有多個EditText,不管你點擊哪個EditText調起軟鍵盤,調用fullScroll(ScrollView.FOCUS_DOWN)方法后都會將焦點給最底部的那個EditText。
解決方法:
1.通過EditText的setOnTouchListener()方法獲取首次點擊的是哪個EditText();
2.在軟鍵盤的監聽方法open()中調用editText.requestFocus();
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LoginActivity";
@BindView(R.id.et_passwork)
EditText password;
@BindView(R.id.et_username)
EditText mobile;
@BindView(R.id.tv_main_title)
private EditText mEdittext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login2);
ButterKnife.bind(this);
inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
KeyboardUtil keyboardUtil = KeyboardUtil.assistActivity(this, R.id.sv_content);
keyboardUtil.setOnKeyBoardOpenListener(new KeyboardUtil.KeyBoardOpenListener() {
@Override
public void open() {
if(mEdittext != null){
mEdittext.requestFocus(); //請求獲取焦點
}
}
@Override
public void close() {
}
});
mobile.setOnTouchListener(new MyOnTouchListener());
password.setOnTouchListener(new MyOnTouchListener());
}
/**
* Edittext的監聽事件
*/
class MyOnTouchListener implements View.OnTouchListener {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (v.getId()){
case R.id.et_username:
mEdittext = mobile;
Log.e(TAG, "onTouch: mobile點擊了");
break;
case R.id.et_passwork:
mEdittext = password;
Log.e(TAG, "onTouch: password點擊了");
break;
}
return false;
}
}
}
方法二:使用scrollTo(0, 100)開新線程:
在軟鍵盤彈起方法中調用
//這里不開新線程的話,會出現點擊第二個輸入框不頂上去的bug,
//初步估計是mScrollView.scrollTo(0, 1000);的原因 最好使用handler開新線程
new Thread(){
@Override
public void run() {
mScrollView.scrollBy(0, 1000);
}
}.start();