注意:本項(xiàng)目還有一個(gè)小坑诈闺。第一次進(jìn)去的時(shí)候有時(shí)候輸入框沒有得到焦點(diǎn),沒有彈出軟鍵盤铃芦,所以不能更好的量取軟鍵盤高度雅镊,給了一個(gè)默認(rèn)值787 軟鍵盤默認(rèn)高度。所以可以忽略不計(jì)刃滓,一旦彈出了軟鍵盤仁烹,這個(gè)高度就被記錄下來了,存在本地咧虎,以便下一次用卓缰。
這幾天沒事,想到之前做im聊天的時(shí)候砰诵,表情輸入和鍵盤之間的切換體驗(yàn)有些問題征唬,看了微信的,覺得真好茁彭,就有了想描摹一下的心思总寒,所有有了這個(gè)demo。站在巨人的肩膀上理肺,我們才能走得更遠(yuǎn)摄闸。
一些配置,導(dǎo)入相關(guān)的庫:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile project(':library')
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'//butterknife注解框架
compile 'com.android.support:support-v4:23.1.1'
}
鍵盤彈出方案
android:windowSoftInputMode="stateVisible|adjustResize"
提一下emoji表情輸入,其實(shí)很簡單哲嘲。
調(diào)用兩個(gè)接口
EmojiconGridFragment.OnEmojiconClickedListener//點(diǎn)擊表情接口
EmojiconsFragment.OnEmojiconBackspaceClickedListener//刪除表情接口
然后實(shí)現(xiàn)一下方法贪薪,emoji就好了
@Override
public void onEmojiconBackspaceClicked(View v) {
EmojiconsFragment.backspace(emojiEditTextView);
}
@Override
public void onEmojiconClicked(Emojicon emojicon) {
EmojiconsFragment.input(emojiEditTextView, emojicon);
}
在運(yùn)用前,要知道這個(gè)公式:
KeyBoard_H = Screen_H - StatusBar_H - AppRect_H
軟鍵盤高度 = 分辨率高 - 狀態(tài)欄高 - 應(yīng)用可視高
于是有了這個(gè)方法
public static int getKeyboardHeight(Activity paramActivity) {
int height = SystemUtils.getScreenHeight(paramActivity) - SystemUtils.getStatusBarHeight(paramActivity)
- SystemUtils.getAppHeight(paramActivity);
if (height == 0) {
height = SharedPreferencesUtils.getIntShareData("KeyboardHeight", 787);//787為默認(rèn)軟鍵盤高度 基本差不離
}else{
SharedPreferencesUtils.putIntShareData("KeyboardHeight", height);
}
return height;
}
主頁布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:emojicon="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.wobiancao.keyboarddemo.MainActivity"
tools:showIn="@layout/activity_main">
<LinearLayout
android:id="@+id/emojicons_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
<com.rockerhieu.emojicon.EmojiconEditText
android:id="@+id/emojicons_edit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="left|top"
android:padding="8dp"
android:textSize="18sp"
emojicon:emojiconSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="?attr/colorPrimary"
android:gravity="center">
<ImageView
android:id="@+id/emojicons_icon"
android:layout_width="40dip"
android:layout_height="40dip"
android:padding="8dip"
android:src="@mipmap/ic_emoticon" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:id="@+id/emojicons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"></RelativeLayout>
</LinearLayout>
剩下略提一下軟鍵盤和輸入框的切換
@OnClick({R.id.emojicons_icon, R.id.emojicons_edit})
void onClick(View view) {
switch (view.getId()){
case R.id.emojicons_icon://點(diǎn)擊表情圖標(biāo),如果表情顯示眠副,隱藏表情画切,打開軟鍵盤。反之囱怕,顯示表情霍弹,隱藏鍵盤
if (emojiconsLayout.isShown()) {
hideEmotionView(true);
} else {
showEmotionView(SystemUtils.isKeyBoardShow(this));
}
break;
case R.id.emojicons_edit://點(diǎn)擊輸入框毫别,打開軟鍵盤,隱藏表情
hideEmotionView(true);
break;
default:
break;
}
}
/**
* 隱藏emoji
**/
private void hideEmotionView(boolean showKeyBoard) {
if (emojiconsLayout.isShown()) {
if (showKeyBoard) {
LinearLayout.LayoutParams localLayoutParams = (LinearLayout.LayoutParams) emojiconsContainer.getLayoutParams();
localLayoutParams.height = emojiconsLayout.getTop();
localLayoutParams.weight = 0.0F;
emojiconsLayout.setVisibility(View.GONE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
SystemUtils.showKeyBoard(editEmojicon);
editEmojicon.postDelayed(new Runnable() {
@Override
public void run() {
unlockContainerHeightDelayed();
}
}, 200L);
} else {
emojiconsLayout.setVisibility(View.GONE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
unlockContainerHeightDelayed();
}
}
}
private void showEmotionView(boolean showAnimation) {
if (showAnimation) {
transitioner.setDuration(200);
} else {
transitioner.setDuration(0);
}
emotionHeight = SystemUtils.getKeyboardHeight(this);
SystemUtils.hideSoftInput(editEmojicon);
emojiconsLayout.getLayoutParams().height = emotionHeight;
emojiconsLayout.setVisibility(View.VISIBLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
//在5.0有navigationbar的手機(jī)典格,高度高了一個(gè)statusBar
int lockHeight = SystemUtils.getAppContentHeight(this);
lockContainerHeight(lockHeight);
}
然后demo里面用到了開源emoji項(xiàng)目岛宦,貼上開源地址,表示感謝
emoji表情開源:https://github.com/rockerhieu/emojicon
本demo開源地址 :https://github.com/a12a15a05/KeyBoardDemo
有bug或問題耍缴,歡迎探討砾肺,謝謝