思路:
放一個(gè)布局在頁面底部,隱藏起來。當(dāng)輸入法彈起來的時(shí)候,檢測(cè)到布局的變化,設(shè)置插件布局的顯示即可
- 在
AndroidManifest.xml里相應(yīng)的Activity里加上android:windowSoftInputMode="stateHidden|adjustResize"
至于為什么是adjustResize而不是adjustSpan,看圖
平時(shí)是這樣
normal.png
adjustResize是這樣

adjustResize.png
adjustPan是這樣

adjustPan.png
區(qū)別:
EditText本來在背景的L處
adjustResize會(huì)把EditText頂?shù)紾處
adjustPan,EditText還是在L處,輸入法把所有布局都頂起來了。
當(dāng)然這倆還有一些別的區(qū)別,這不是本文討論的重點(diǎn)。
上代碼
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:hint="XXXXXXXXXXXX"/>
<TextView
android:id="@+id/tv"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="20sp"
android:background="@android:color/holo_green_light"
android:text="bbbbbbbbbbbbbbbbbbbbbb"
android:visibility="invisible"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A\nB\nC\nD\nE\nD\nF\nG\nH\nI\nJ\nK\nL\nM\nN"
android:textSize="60sp"/>
</RelativeLayout>
代碼
public class MainActivity extends AppCompatActivity {
private TextView mTv;
private EditText mEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv = (TextView) findViewById(R.id.tv);
mEt = (EditText) findViewById(R.id.et);
getWindow().getDecorView().getViewTreeObserver()
.addOnGlobalLayoutListener(mLayoutChangeListener);
}
ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//判斷窗口可見區(qū)域大小
Rect r = new Rect();
// getWindowVisibleDisplayFrame()會(huì)返回窗口的可見區(qū)域高度
getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
int mScreenHeight = ScreenUtils.getScreenHeight();
//如果屏幕高度和Window可見區(qū)域高度差值大于整個(gè)屏幕高度的1/3
// 則表示軟鍵盤顯示中,否則軟鍵盤為隱藏狀態(tài)。
int heightDifference = mScreenHeight - (r.bottom - r.top);
boolean isKeyboardShowing = heightDifference > mScreenHeight / 3;
if(isKeyboardShowing){
mTv.setVisibility(View.VISIBLE);
}else{
mTv.setVisibility(View.INVISIBLE);
}
}
};
}
為復(fù)用方便,拎了一個(gè)工具類出來
public class KeyBoardPluginUtils {
/**
* 這是全局的,用addOnGlobalLayoutListener,只要輸入法彈起來就會(huì)觸發(fā)
* 清單里配置android:windowSoftInputMode="stateHidden|adjustResize"
* */
public static void addKeyBoardPluginGlobal(Activity activity,LinearLayout llPlugin) {
activity.getWindow().getDecorView().getViewTreeObserver()
.addOnGlobalLayoutListener(new GlobalLayoutListener(activity,llPlugin));
}
/**
* 指定某個(gè)EditText會(huì)觸發(fā)
*/
public static void addKeyBoardPlugin(EditText et, final LinearLayout llPlugin) {
et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
llPlugin.setVisibility(View.VISIBLE);
}else {
llPlugin.setVisibility(View.INVISIBLE);
}
}
});
}
private static class GlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
private Activity mActivity;
private LinearLayout mLlPlugin;
private GlobalLayoutListener(Activity activity, LinearLayout llPlugin) {
mActivity = activity;
mLlPlugin = llPlugin;
}
@Override
public void onGlobalLayout() {
//判斷窗口可見區(qū)域大小
Rect r = new Rect();
// getWindowVisibleDisplayFrame()會(huì)返回窗口的可見區(qū)域高度
mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
int mScreenHeight = ScreenUtils.getScreenHeight();
//如果屏幕高度和Window可見區(qū)域高度差值大于整個(gè)屏幕高度的1/3
// 則表示軟鍵盤顯示中,否則軟鍵盤為隱藏狀態(tài)。
int heightDifference = mScreenHeight - (r.bottom - r.top);
boolean isKeyboardShowing = heightDifference > mScreenHeight / 3;
if(isKeyboardShowing){
mLlPlugin.setVisibility(View.VISIBLE);
}else{
mLlPlugin.setVisibility(View.INVISIBLE);
}
}
}
}
