Android鍵盤面板沖突 布局閃動處理方案

JanMcAllist 8年前發布 | 40K 次閱讀 Android 鍵盤 Android開發 移動開發

起源,之前在微信工作的時候,為了給用戶帶來更好的基礎體驗,做了很多嘗試,踩了很多輸入法的坑,特別是動態調整鍵盤高度,二級頁面是透明背景,魅族早期的Smart bar等, 后來逐一完善了,考慮到擁抱開源,看業界還是有很多應用存在類似問題。就有了這個repo

之前有寫過一篇核心思想: Switching between the panel and the keyboard in Wechat

Android鍵盤面板沖突 布局閃動處理方案Android鍵盤面板沖突 布局閃動處理方案 Android鍵盤面板沖突 布局閃動處理方案Android鍵盤面板沖突 布局閃動處理方案

歡迎提交 Pull requests

  • 盡量多的英文注解。
  • 每個提交盡量的細而精準。
  • Commit message 遵循: AngularJS's commit message convention
  • 盡可能的遵循IDE的代碼檢查建議(如 Android Studio 的 'Inspect Code')。

如何使用

build.gradle中引入:

compile 'cn.dreamtobe.kpswitch:library:1.4.4'

使用引導

非全屏主題情況下使用引導

所謂非全屏主題,就是 (activity.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) == 0

Android鍵盤面板沖突 布局閃動處理方案

I. AndroidManifest

可直接參照: AndroidManifest.xml

對應的Activity,在AndroidManifest中配置android:windowSoftInputMode=adjustResize

<manifest
  ...>
  <application
    ...>

<activity
  android:name=".activity.ChattingActivity"
  android:windowSoftInputMode=adjustResize"/>
  ...

</application> ... </manifest> </code></pre>

II. 需要處理頁面的layout xml

可直接參照: activity_chatting_resolved.xml

  1. 需要用到 最上層布局 (KPSwitchRootFrameLayout/KPSwitchRootLinearLayout/KPSwitchRootRelativeLayout)
  2. 需要用到 面板布局(KPSwitchPanelFrameLayout/KPSwitchPanelLinearLayout/KPSwitchPanelRelativeLayout)。

簡單案例:

<?xml version="1.0" encoding="utf-8"?>
<!-- 可選用 KPSwitchRootLinearLayout、KPSwitchRootRelativeLayout、KPSwitchRootFrameLayout -->
<cn.dreamtobe.kpswitch.widget.KPSwitchRootLinearLayout xmlns:android="

<!-- 布局內容 -->
...

<!-- 可選用 KPSwitchPanelLinearLayout、KPSwitchPanelRelativeLayout、KPSwitchPanelFrameLayout -->
<cn.dreamtobe.kpswitch.widget.KPSwitchPanelLinearLayout
    android:id="@+id/panel_root"
    android:layout_width="fill_parent"
    android:layout_height="@dimen/panel_height"
    android:visibility="gone">
    <!-- 面板內容 -->
    ...
</cn.dreamtobe.kpswitch.widget.KPSwitchPanelLinearLayout>

</cn.dreamtobe.kpswitch.widget.KPSwitchRootLinearLayout> </code></pre>

III. 需要處理頁面的Activity:

可直接參照: ChattingResolvedActivity.java

  1. 處理一些事件(KPSwitchConflictUtil)
  2. 鍵盤狀態(高度與顯示與否)監聽(KeyboardUtil#attach())

簡單案例:

...

// 面板View private KPSwitchPanelLinearLayout mPanelLayout; // 鍵盤焦點View,用于輸入內容 private EditText mSendEdt; // 用于切換鍵盤與面板的按鈕View private ImageView mPlusIv;

@Override public void onCreate(Bundle saveInstanceState){ ...

mPanelLayout = (KPSwitchPanelLinearLayout)findViewById(R.id.panel_root);
mSendEdt = (EditText) findViewById(R.id.send_edt);
mPlusIv = (ImageView) findViewById(R.id.plus_iv);

/**
 * 這個Util主要是監控鍵盤的狀態: 顯示與否 以及 鍵盤的高度
 * 這里也有提供給外界監聽 鍵盤顯示/隱藏 的監聽器,具體參看
 * 這個接口 {@Link KeyboardUtil#attach(Activity, IPanelHeightTarget, OnKeyboardShowingListener)}
 */
KeyboardUtil.attach(this, mPanelLayout);

/**
 * 這個Util主要是協助處理一些面板與鍵盤相關的事件。
 * 這個方法主要是對一些相關事件進行注冊,如切換面板與鍵盤等,具體參看源碼,比較簡單。
 * 里面還提供了一些已經處理了沖突的工具方法: 顯示面板;顯示鍵盤;鍵盤面板切換;隱藏鍵盤與面板;
 *
 * @param panelRoot 面板的布局。
 * @param switchPanelKeyboardBtn 用于觸發切換面板與鍵盤的按鈕。
 * @param focusView 鍵盤彈起時會給這個View focus,收回時這個View會失去focus,通常是發送的EditText。
 */
KPSwitchConflictUtil.attach(mPanelLayout, mPlusIv, mSendEdt);

}

...

...

// 如果需要處理返回收起面板的話 @Override public boolean dispatchKeyEvent(KeyEvent event){ if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (mPanelLayout.getVisibility() == View.VISIBLE) { KPSwitchConflictUtil.hidePanelAndKeyboard(mPanelLayout); return true; } } return super.dispatchKeyEvent(event); }</code></pre>

全屏主題情況下使用引導

所謂全屏主題,就是 (activity.getWindow().getAttributes().flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0

Android鍵盤面板沖突 布局閃動處理方案

I. AndroidManifest

可直接參照: AndroidManifest.xml

對應的Activity,在 AndroidManifest中配置 android:windowSoftInputMode=adjustUnspecified,或者不配置,默認就是這個模式。

II. 需要處理頁面的layout xml

可直接參照: activity_chatting_fullscreen_resolved.xml

這邊只需要用到一個 面板布局(KPSwitchFSPanelFrameLayout/KPSwitchFSPanelLinearLayout/KPSwitchFSPanelRelativeLayout)

<?xml version="1.0" encoding="utf-8"?>
...
    ...

<!-- 可選用 KPSwitchFSPanelFrameLayout、KPSwitchFSPanelLinearLayout、KPSwitchFSPanelRelativeLayout -->
<cn.dreamtobe.kpswitch.widget.KPSwitchFSPanelFrameLayout
    android:id="@+id/panel_root"
    style="@style/Panel"
    android:visibility="gone">

    ...
</cn.dreamtobe.kpswitch.widget.KPSwitchFSPanelFrameLayout>

... </code></pre>

III. 需要處理頁面的Activity:

可直接參照: ChattingResolvedFullScreenActivity.java

  1. 主要是處理一些事件(KPSwitchConflictUtil)
  2. 鍵盤狀態(高度與顯示與否)監聽(KeyboardUtil#attach())
  3. onPause時,記錄鍵盤狀態用于從后臺回到當前布局,恢復鍵盤狀態不至于沖突(IFSPanelConflictLayout#recordKeyboardStatus())

如下使用案例:

...

// 面板View private KPSwitchFSPanelLinearLayout mPanelLayout; // 鍵盤焦點View,用于輸入內容 private EditText mSendEdt; // 用于切換鍵盤與面板的按鈕View private ImageView mPlusIv;

@Override public void onCreate(Bundle saveInstanceState){ ...

mPanelLayout = (KPSwitchFSPanelLinearLayout)findViewById(R.id.panel_root);
mSendEdt = (EditText) findViewById(R.id.send_edt);
mPlusIv = (ImageView) findViewById(R.id.plus_iv);

/**
 * 這個Util主要是監控鍵盤的狀態: 顯示與否 以及 鍵盤的高度
 * 這里也有提供給外界監聽 鍵盤顯示/隱藏 的監聽器,具體參看
 * 這個接口 {@Link KeyboardUtil#attach(Activity, IPanelHeightTarget, OnKeyboardShowingListener)}
 */
KeyboardUtil.attach(this, mPanelLayout);

/**
 * 這個Util主要是協助處理一些面板與鍵盤相關的事件。
 * 這個方法主要是對一些相關事件進行注冊,如切換面板與鍵盤等,具體參看源碼,比較簡單。
 * 里面還提供了一些已經處理了沖突的工具方法: 顯示面板;顯示鍵盤;鍵盤面板切換;隱藏鍵盤與面板;
 *
 * @param panelRoot 面板的布局。
 * @param switchPanelKeyboardBtn 用于觸發切換面板與鍵盤的按鈕。
 * @param focusView 鍵盤彈起時會給這個View focus,收回時這個View會失去focus,通常是發送的EditText。
 */
KPSwitchConflictUtil.attach(mPanelLayout, mPlusIv, mSendEdt);

}

@Override protected void onPause() { super.onPause(); // 用于記錄當前的鍵盤狀態,在從后臺回到當前頁面的時候,鍵盤狀態能夠正確的恢復并且不會導致布局沖突。 mPanelLayout.recordKeyboardStatus(getWindow()); }

...

// 如果需要處理返回收起面板的話 @Override public boolean dispatchKeyEvent(KeyEvent event){ if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (mPanelLayout.getVisibility() == View.VISIBLE) { KPSwitchConflictUtil.hidePanelAndKeyboard(mPanelLayout); return true; } } return super.dispatchKeyEvent(event); }</code></pre>

基本原理

 本文由用戶 JanMcAllist 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!