現(xiàn)在很多應用都會在讓用戶輸入各種文本信息的時候同時多提供一個表情面板,這樣就會出現(xiàn)一個問題,即表情面板的跳閃問題要輸入文本信息,那固然是需要彈出軟鍵盤,在軟鍵盤顯示的情況下,此時如果要切換顯示出表情面板,由于表情面板不可能和用戶的軟鍵盤高度恰好一樣,此外由于控件的上下移位,就會出現(xiàn)表情面板的跳閃現(xiàn)象
創(chuàng)新互聯(lián)公司專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、東豐網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、H5場景定制、電子商務商城網(wǎng)站建設、集團公司官網(wǎng)建設、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為東豐等各大城市提供網(wǎng)站開發(fā)制作服務。
在點擊切換按鈕的時候,表情面板會先向上跳,然后再往下移,這樣就會帶來很差的用戶體驗,效果如下圖所示:
這里提供一個解決方案,使軟鍵盤和表情面板可以很自然地切換,效果如下圖所示:
解決思路主要是這樣:系統(tǒng)在彈出軟鍵盤時,會將內容輸入框View以及其之上的View都給頂上去,當切換到表情面板時,只有將表情面板的高度保持為和軟鍵盤的高度一致,才能自然地切換。此外,還需要將內容輸入框View以及其之上的View的位置固定住,這樣才不會出現(xiàn)跳閃問題
主要的操作邏輯都在 EmojiKeyboard 類中
/** * 作者: chenZY * 時間: 2017/8/26 18:12 * 描述: */ public class EmojiKeyboard { private Activity activity; //文本輸入框 private EditText editText; //表情面板 private View emojiPanelView; //內容View,即除了表情布局和輸入框布局以外的布局 //用于固定輸入框一行的高度以防止跳閃 private View contentView; private InputMethodManager inputMethodManager; private SharedPreferences sharedPreferences; private static final String EMOJI_KEYBOARD = "EmojiKeyboard"; private static final String KEY_SOFT_KEYBOARD_HEIGHT = "SoftKeyboardHeight"; private static final int SOFT_KEYBOARD_HEIGHT_DEFAULT = 654; private Handler handler; public EmojiKeyboard(Activity activity, EditText editText, View emojiPanelView, View emojiPanelSwitchView, View contentView) { init(activity, editText, emojiPanelView, emojiPanelSwitchView, contentView); } private void init(Activity activity, EditText editText, View emojiPanelView, View emojiPanelSwitchView, View contentView) { this.activity = activity; this.editText = editText; this.emojiPanelView = emojiPanelView; this.contentView = contentView; this.editText.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(final View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP && EmojiKeyboard.this.emojiPanelView.isShown()) { lockContentViewHeight(); hideEmojiPanel(true); unlockContentViewHeight(); } return false; } }); this.contentView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_UP) { if (EmojiKeyboard.this.emojiPanelView.isShown()) { hideEmojiPanel(false); } else if (isSoftKeyboardShown()) { hideSoftKeyboard(); } } return false; } }); //用于彈出表情面板的View emojiPanelSwitchView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (EmojiKeyboard.this.emojiPanelView.isShown()) { lockContentViewHeight(); hideEmojiPanel(true); unlockContentViewHeight(); } else { if (isSoftKeyboardShown()) { lockContentViewHeight(); showEmojiPanel(); unlockContentViewHeight(); } else { showEmojiPanel(); } } } }); this.inputMethodManager = (InputMethodManager) this.activity.getSystemService(Context.INPUT_METHOD_SERVICE); this.sharedPreferences = this.activity.getSharedPreferences(EMOJI_KEYBOARD, Context.MODE_PRIVATE); this.activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); this.handler = new Handler(); init(); } /** * 如果之前沒有保存過鍵盤高度值 * 則在進入Activity時自動打開鍵盤,并把高度值保存下來 */ private void init() { if (!sharedPreferences.contains(KEY_SOFT_KEYBOARD_HEIGHT)) { handler.postDelayed(new Runnable() { @Override public void run() { showSoftKeyboard(true); } }, 200); } } /** * 當點擊返回鍵時需要先隱藏表情面板 */ public boolean interceptBackPress() { if (emojiPanelView.isShown()) { hideEmojiPanel(false); return true; } return false; } /** * 鎖定內容View以防止跳閃 */ private void lockContentViewHeight() { LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) contentView.getLayoutParams(); layoutParams.height = contentView.getHeight(); layoutParams.weight = 0; } /** * 釋放鎖定的內容View */ private void unlockContentViewHeight() { handler.postDelayed(new Runnable() { @Override public void run() { ((LinearLayout.LayoutParams) contentView.getLayoutParams()).weight = 1; } }, 200); } /** * 獲取鍵盤的高度 */ private int getSoftKeyboardHeight() { Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); //屏幕當前可見高度,不包括狀態(tài)欄 int displayHeight = rect.bottom - rect.top; //屏幕可用高度 int availableHeight = ScreenUtils.getAvailableScreenHeight(activity); //用于計算鍵盤高度 int softInputHeight = availableHeight - displayHeight - ScreenUtils.getStatusBarHeight(activity); Log.e("TAG-di", displayHeight + ""); Log.e("TAG-av", availableHeight + ""); Log.e("TAG-so", softInputHeight + ""); if (softInputHeight != 0) { // 因為考慮到用戶可能會主動調整鍵盤高度,所以只能是每次獲取到鍵盤高度時都將其存儲起來 sharedPreferences.edit().putInt(KEY_SOFT_KEYBOARD_HEIGHT, softInputHeight).apply(); } return softInputHeight; } /** * 獲取本地存儲的鍵盤高度值或者是返回默認值 */ private int getSoftKeyboardHeightLocalValue() { return sharedPreferences.getInt(KEY_SOFT_KEYBOARD_HEIGHT, SOFT_KEYBOARD_HEIGHT_DEFAULT); } /** * 判斷是否顯示了鍵盤 */ private boolean isSoftKeyboardShown() { return getSoftKeyboardHeight() != 0; } /** * 令編輯框獲取焦點并顯示鍵盤 */ private void showSoftKeyboard(boolean saveSoftKeyboardHeight) { editText.requestFocus(); inputMethodManager.showSoftInput(editText, 0); if (saveSoftKeyboardHeight) { handler.postDelayed(new Runnable() { @Override public void run() { getSoftKeyboardHeight(); } }, 200); } } /** * 隱藏鍵盤 */ private void hideSoftKeyboard() { inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0); } /** * 顯示表情面板 */ private void showEmojiPanel() { int softKeyboardHeight = getSoftKeyboardHeight(); if (softKeyboardHeight == 0) { softKeyboardHeight = getSoftKeyboardHeightLocalValue(); } else { hideSoftKeyboard(); } emojiPanelView.getLayoutParams().height = softKeyboardHeight; emojiPanelView.setVisibility(View.VISIBLE); if (emojiPanelVisibilityChangeListener != null) { emojiPanelVisibilityChangeListener.onShowEmojiPanel(); } } /** * 隱藏表情面板,同時指定是否隨后開啟鍵盤 */ private void hideEmojiPanel(boolean showSoftKeyboard) { if (emojiPanelView.isShown()) { emojiPanelView.setVisibility(View.GONE); if (showSoftKeyboard) { showSoftKeyboard(false); } if (emojiPanelVisibilityChangeListener != null) { emojiPanelVisibilityChangeListener.onHideEmojiPanel(); } } } public interface OnEmojiPanelVisibilityChangeListener { void onShowEmojiPanel(); void onHideEmojiPanel(); } private OnEmojiPanelVisibilityChangeListener emojiPanelVisibilityChangeListener; public void setEmoticonPanelVisibilityChangeListener(OnEmojiPanelVisibilityChangeListener emojiPanelVisibilityChangeListener) { this.emojiPanelVisibilityChangeListener = emojiPanelVisibilityChangeListener; } }
這里也提供代碼下載:Android 解決表情面板和軟鍵盤切換時跳閃的問題
總結
以上所述是小編給大家介紹的Android 表情面板和軟鍵盤切換時跳閃問題的解決方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!