Android 处理软键盘遮挡问题
生活随笔
收集整理的這篇文章主要介紹了
Android 处理软键盘遮挡问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- Android 處理軟鍵盤遮擋問題
- 方案一:使用NestedScrollView
- 方案說明
- 方案二:使用scrollTo或屬性動畫
- 方案說明
- 代碼下載
Android 處理軟鍵盤遮擋問題
方案一:使用NestedScrollView
方案說明
使用NestedScrollView嵌套時,當鍵盤彈出時,頂層的DecorView的高度是不變的,而ContentView的高度則會縮小。
通過這個特性可以監聽ContentView來判斷軟鍵盤的打開和關閉。
XML布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"tools:context=".LoginActivity"><ImageViewandroid:id="@+id/logo"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerHorizontal="true"android:layout_marginTop="120dp"android:src="@mipmap/ic_launcher_round" /><androidx.core.widget.NestedScrollViewandroid:id="@+id/scrollView"android:layout_width="match_parent"android:layout_height="match_parent"android:fillViewport="true"android:scrollbarThumbVertical="@android:color/transparent"android:scrollbars="vertical"><LinearLayoutandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="15dp"android:layout_marginRight="15dp"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="300dp"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:layout_width="35dp"android:layout_height="35dp"android:src="@drawable/user" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="請輸入用戶名" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:layout_width="35dp"android:layout_height="35dp"android:src="@drawable/pwd" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="請輸入密碼" /></LinearLayout><Buttonandroid:id="@+id/confirm"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:background="#FF0000"android:text="確定" /><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:text="忘記密碼" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="注冊" /></RelativeLayout></LinearLayout></androidx.core.widget.NestedScrollView><LinearLayoutandroid:id="@+id/helper"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:gravity="center"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="聯系客服" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:text="|" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="關于我們" /></LinearLayout></RelativeLayout>邏輯代碼
class LoginActivity : AppCompatActivity(), View.OnLayoutChangeListener {private lateinit var scrollView: NestedScrollViewprivate lateinit var helper: LinearLayoutprivate lateinit var content: LinearLayoutprivate lateinit var logo: ImageViewprivate lateinit var confirm: Buttonprivate var screenHeight = 0 //屏幕高度private var keyboardHeight = 0 //鍵盤高度override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_login)scrollView = findViewById(R.id.scrollView)helper = findViewById(R.id.helper)content = findViewById(R.id.content)logo = findViewById(R.id.logo)confirm = findViewById(R.id.confirm)confirm.setOnClickListener { KeyboardUtils.hideKeyboard(confirm) }screenHeight = Resources.getSystem().displayMetrics.heightPixelskeyboardHeight = screenHeight / 3//禁止ScrollView滾動scrollView.setOnTouchListener { v, event -> true }//監聽scrollView.addOnLayoutChangeListener(this)}override fun onLayoutChange(v: View?,left: Int, top: Int, right: Int, bottom: Int,oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {if (oldBottom == 0 || bottom == 0)returnif ((oldBottom - bottom) > keyboardHeight) {Log.e("TAG", "軟鍵盤彈出")val diff = content.bottom - bottomif (diff > 0) {AnimatorUtils.translationY(content, 0F, (-diff).toFloat())AnimatorUtils.scaleXY(logo, 1F, 0.6F)AnimatorUtils.translationY(logo, 0F, -100F)}helper.visibility = View.INVISIBLE} else if ((bottom - oldBottom) > keyboardHeight) {Log.e("TAG", "軟鍵盤收起")val diff = content.bottom - oldBottomif (diff > 0) {AnimatorUtils.translationY(content, content.translationY, 0F)AnimatorUtils.scaleXY(logo, 0.6F, 1F)AnimatorUtils.translationY(logo, logo.translationY, 0F)}helper.visibility = View.VISIBLE}}override fun onDestroy() {super.onDestroy()scrollView.removeOnLayoutChangeListener(this)} }動畫代碼
object AnimatorUtils {/*** Y軸移動動畫*/fun translationY(view: View, from: Float, to: Float) {ObjectAnimator.ofFloat(view, TRANSLATION_Y, from, to).apply {interpolator = LinearInterpolator()duration = 300L}.start()}/*** 縮放動畫*/fun scaleXY(view: View, fromScale: Float, toScale: Float) {view.apply {pivotX = (view.width / 2).toFloat()pivotY = (view.height / 2).toFloat()}val animatorScaleX = ObjectAnimator.ofFloat(view, SCALE_X, fromScale, toScale)val animatorScaleY = ObjectAnimator.ofFloat(view, SCALE_Y, fromScale, toScale)AnimatorSet().apply {duration = 300LplayTogether(animatorScaleX, animatorScaleY);}.start()} }方案二:使用scrollTo或屬性動畫
方案說明
通過getWindowVisibleDisplayFrame()獲取window的可視區域,軟鍵盤彈出時,可視區域的高度小于decorView的高度。
監聽軟鍵盤狀態工具類
/*** 監聽頂層View,判斷軟鍵盤是否彈出*/ public class SoftKeyboardListener implements ViewTreeObserver.OnGlobalLayoutListener {private static SoftKeyboardListener mSoftKeyboardListener;private boolean isShowKeyboard;private View decorView;private OnSoftKeyboardListener mListener;public static SoftKeyboardListener with(@NotNull Activity activity) {if (mSoftKeyboardListener == null) {mSoftKeyboardListener = new SoftKeyboardListener(activity);}return mSoftKeyboardListener;}public SoftKeyboardListener(Activity activity) {isShowKeyboard = false;decorView = activity.getWindow().getDecorView();}@Overridepublic void onGlobalLayout() {if (mListener == null) return;Rect rect = new Rect();decorView.getWindowVisibleDisplayFrame(rect);int visibleHeight = rect.height();int diffHeight = decorView.getHeight() - visibleHeight;if (!isShowKeyboard && diffHeight > decorView.getRootView().getHeight() / 4) {isShowKeyboard = true;mListener.onKeyboardShow(diffHeight);} else if (isShowKeyboard && diffHeight < decorView.getRootView().getHeight() / 4) {isShowKeyboard = false;mListener.onKeyboardHide();}}/*** 注冊監聽*/public void registerListener(@NotNull OnSoftKeyboardListener listener) {mListener = listener;decorView.getViewTreeObserver().addOnGlobalLayoutListener(this);Log.e("TAG", "register " + this.hashCode());}/*** 取消監聽*/public void unregisterListener() {if (mSoftKeyboardListener != null) {Log.e("TAG", "unregister " + this.hashCode());decorView.getViewTreeObserver().removeOnGlobalLayoutListener(this);mListener = null;decorView = null;isShowKeyboard = false;mSoftKeyboardListener = null;}}public interface OnSoftKeyboardListener {/*** 軟鍵盤彈出** @param keyboardHeight 軟鍵盤高度*/void onKeyboardShow(int keyboardHeight);/*** 軟鍵盤收起*/void onKeyboardHide();} }XML布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"tools:context=".LoginActivity"><ImageViewandroid:id="@+id/logo"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerHorizontal="true"android:layout_marginTop="120dp"android:src="@mipmap/ic_launcher_round" /><LinearLayoutandroid:id="@+id/content"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="15dp"android:layout_marginRight="15dp"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="300dp"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:layout_width="35dp"android:layout_height="35dp"android:src="@drawable/user" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="請輸入用戶名" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:layout_width="35dp"android:layout_height="35dp"android:src="@drawable/pwd" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="請輸入密碼" /></LinearLayout><Buttonandroid:id="@+id/confirm"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="確定" /><RelativeLayoutandroid:id="@+id/help"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:text="忘記密碼" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="注冊" /></RelativeLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:gravity="center"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="聯系客服" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:text="|" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="關于我們" /></LinearLayout></RelativeLayout>邏輯代碼
class LoginActivity2 : AppCompatActivity() {private lateinit var content: LinearLayoutprivate lateinit var help: Viewprivate lateinit var confirm: Buttonprivate lateinit var logo: ImageViewprivate var diff = 0override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_login2)content = findViewById(R.id.content)confirm = findViewById(R.id.confirm)help = findViewById(R.id.help)logo = findViewById(R.id.logo)help.post {diff = help.bottom - confirm.top}SoftKeyboardListener.with(this).registerListener(object : OnSoftKeyboardListener {override fun onKeyboardShow(keyboardHeight: Int) {Log.e("TAG", "軟鍵盤彈出")AnimatorUtils.scaleXY(logo, 1F, 0.6F)AnimatorUtils.translationY(logo, 0F, -100F) // content.scrollBy(0, diff)AnimatorUtils.translationY(content, 0F, -diff.toFloat())}override fun onKeyboardHide() {Log.e("TAG", "軟鍵盤收起")AnimatorUtils.scaleXY(logo, 0.6F, 1F)AnimatorUtils.translationY(logo, logo.translationY, 0F) // content.scrollTo(0, 0)AnimatorUtils.translationY(content, -diff.toFloat(), 0F)}})}override fun onDestroy() {super.onDestroy()SoftKeyboardListener.with(this).unregisterListener()} }代碼下載
總結
以上是生活随笔為你收集整理的Android 处理软键盘遮挡问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python求最小公倍数_Python实
- 下一篇: 思岚科技—SLAMTEC将自家研发技术应