Android幾行程式碼解決鍵盤遮擋問題
將libray模組複製到專案中,或者直接在build.gradle中依賴:
allprojects { repositories { maven { url 'https://jitpack.io' } } }
dependencies { compile 'com.github.AnJiaoDe:Keyboard:V1.0.0' }
注意:如果sync報錯,是因為和com.android.tools.build:gradle 3.0有關,
可以改將compile改為implementation 或者api
1.scrollTo、scrollBy實現鍵盤不遮擋

1.gif
使用方法:
public class EditTextActivity extends KeyboardActivity { private View layout_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_edit_text); layout_content=findViewById(R.id.layout_content); //設定監聽器 setOnCYGlobalLayoutListener(new OnCYGlobalLayoutListener() { //鍵盤剛顯示 @Override public void onKeyboardShowedNow(Rect rect) { //rect 可視區域 //screenHight-rect.bottom就是鍵盤高度 layout_content.scrollTo(0,screenHight-rect.bottom); } //鍵盤剛隱藏,可以對輸入的內容進行修改 @Override public void onKeyboardHideNow() { layout_content.scrollTo(0,0); } }); } }
2.列表中的Edittext,利用假鍵盤實現鍵盤不遮擋

2.gif
使用方法:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_modify_price2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:background="@color/colorPrimary" android:layout_width="match_parent" android:layout_height="44dp"> <TextView android:layout_width="match_parent" android:gravity="center" android:layout_height="match_parent" android:text="修改價格" android:textColor="#ffffff" /> </RelativeLayout> <com.cy.cyrvadapter.recyclerview.VerticalRecyclerView android:id="@+id/vrv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <View android:id="@+id/view_keyboard" android:layout_width="match_parent" android:layout_height="0dp" /> </LinearLayout>
public class RVEdittextActivity extends KeyboardMockActivity { private RVAdapter<String> rvAdapter; private View view_keybord; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_rvedittext); List<String> list = new ArrayList<>(); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); list.add("dojij"); rvAdapter = new RVAdapter<String>(list) { @Override public void bindDataToView(RVViewHolder holder, int position, String bean, boolean isSelected) { } @Override public int getItemLayoutID(int position, String bean) { return R.layout.item_price_modify; } @Override public void onItemClick(int position, String bean) { } }; //萬能介面卡:https://github.com/AnJiaoDe/RecyclerViewAdapter ((VerticalRecyclerView) findViewById(R.id.vrv)).setAdapter(rvAdapter); //設定監聽器 setOnCYGlobalLayoutListener(view_keybord, new OnCYGlobalLayoutListener() { //鍵盤剛顯示 @Override public void onKeyboardShowedNow(Rect rect) { } //鍵盤剛隱藏,可以對輸入的內容進行修改 @Override public void onKeyboardHideNow() { } //假鍵盤高度 @Override public int getViewKeyboardHeight(Rect rect) { return screenHight-rect.bottom;//screenHight-rect.bottom就是鍵盤高度 } }); } }
原始碼:
/** * Created by cy on 2018/9/6.,用在普通介面中的edittext */ public abstract class KeyboardActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener { public int screenHight = 0; private OnCYGlobalLayoutListener onCYGlobalLayoutListener; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); screenHight = ScreenUtils.getScreenHeight(getApplicationContext()); } @Override protected void onResume() { super.onResume(); addGlobal(); } @Override protected void onStop() { super.onStop(); removeGlobal(); } private void addGlobal() { if (onCYGlobalLayoutListener!=null) getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(this); } private void removeGlobal() { getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { //CYLogUtils.log("佈局變化", "---------------------------------------"); Rect r = new Rect(); //獲取當前介面可視部分 getWindow().getDecorView().getWindowVisibleDisplayFrame(r); //如果螢幕高度和Window可見區域高度差值大於整個螢幕高度的1/3,則表示軟鍵盤顯示中,否則軟鍵盤為隱藏狀態。 //int heightDifference = screenHeight - (r.bottom - r.top); boolean isKeyboardShowing = r.bottom < ScreenUtils.getScreenHeight(getApplicationContext()); if (isKeyboardShowing) { //Toast.makeText(this, "鍵盤顯示", Toast.LENGTH_SHORT).show(); removeGlobal(); onCYGlobalLayoutListener.onKeyboardShowedNow(r); //會導致佈局不斷重新整理,無限制回調當前方法,所以做延遲迴調處理 new Handler().postDelayed(new Runnable() { @Override public void run() { addGlobal(); } }, 100); } else { //Toast.makeText(this, "鍵盤隱藏", Toast.LENGTH_SHORT).show(); removeGlobal(); onCYGlobalLayoutListener.onKeyboardHideNow(); //會導致佈局不斷重新整理,無限制回調當前方法,所以做延遲迴調處理 new Handler().postDelayed(new Runnable() { @Override public void run() { addGlobal(); } }, 100); } } public int getScreenHight() { return screenHight; } /** * @param onCYGlobalLayoutListener */ public void setOnCYGlobalLayoutListener( OnCYGlobalLayoutListener onCYGlobalLayoutListener) { this.onCYGlobalLayoutListener = onCYGlobalLayoutListener; addGlobal(); } public interface OnCYGlobalLayoutListener { public void onKeyboardShowedNow(Rect rect);//鍵盤剛顯示 public void onKeyboardHideNow();//鍵盤剛隱藏 } }
/** * Created by cy on 2018/9/6.,假鍵盤,用在列表中的edittext */ public abstract class KeyboardMockActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener { private View view_keyboard; public int screenHight = 0; private OnCYGlobalLayoutListener onCYGlobalLayoutListener; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); screenHight = ScreenUtils.getScreenHeight(getApplicationContext()); } @Override protected void onResume() { super.onResume(); addGlobal(); } @Override protected void onStop() { super.onStop(); removeGlobal(); } private void addGlobal() { if (view_keyboard == null) return; getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(this); } private void removeGlobal() { getWindow().getDecorView().getViewTreeObserver().removeOnGlobalLayoutListener(this); } @Override public void onGlobalLayout() { if (view_keyboard == null) { removeGlobal(); return; } //CYLogUtils.log("佈局變化", "---------------------------------------"); Rect r = new Rect(); //獲取當前介面可視部分 getWindow().getDecorView().getWindowVisibleDisplayFrame(r); //如果螢幕高度和Window可見區域高度差值大於整個螢幕高度的1/3,則表示軟鍵盤顯示中,否則軟鍵盤為隱藏狀態。 //int heightDifference = screenHeight - (r.bottom - r.top); boolean isKeyboardShowing = r.bottom < ScreenUtils.getScreenHeight(getApplicationContext()); if (isKeyboardShowing) { if (view_keyboard.getVisibility() == View.VISIBLE) return; //Toast.makeText(this, "鍵盤顯示", Toast.LENGTH_SHORT).show(); removeGlobal(); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view_keyboard.getLayoutParams(); layoutParams.height = onCYGlobalLayoutListener.getViewKeyboardHeight(r); view_keyboard.setVisibility(View.VISIBLE); onCYGlobalLayoutListener.onKeyboardShowedNow(r); //會導致佈局不斷重新整理,無限制回調當前方法,所以做延遲迴調處理 new Handler().postDelayed(new Runnable() { @Override public void run() { addGlobal(); } }, 100); } else { //Toast.makeText(this, "鍵盤隱藏", Toast.LENGTH_SHORT).show(); if (view_keyboard.getVisibility() == View.GONE) return; removeGlobal(); view_keyboard.setVisibility(View.GONE); onCYGlobalLayoutListener.onKeyboardHideNow(); //會導致佈局不斷重新整理,無限制回調當前方法,所以做延遲迴調處理 new Handler().postDelayed(new Runnable() { @Override public void run() { addGlobal(); } }, 100); } } public int getScreenHight() { return screenHight; } /** * @param view_keyboard假鍵盤佈局,使用時必須自行設定其高度,以滿足 需要 * @param onCYGlobalLayoutListener */ public void setOnCYGlobalLayoutListener(View view_keyboard, OnCYGlobalLayoutListener onCYGlobalLayoutListener) { this.view_keyboard = view_keyboard; this.onCYGlobalLayoutListener = onCYGlobalLayoutListener; addGlobal(); } public interface OnCYGlobalLayoutListener { public void onKeyboardShowedNow(Rect rect);//鍵盤剛顯示 public void onKeyboardHideNow();//鍵盤剛隱藏 public int getViewKeyboardHeight(Rect rect);//返回假鍵盤的高度,rect 可視區域 } }
關注專題 Android開發常用開源庫
微信公眾號

這裡寫圖片描述
QQ群

這裡寫圖片描述