├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── leavesc │ │ └── hello │ │ └── keyboard │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── leavesc │ │ │ └── hello │ │ │ └── keyboard │ │ │ ├── EmojiKeyboard.java │ │ │ ├── MainActivity.java │ │ │ ├── ResolvedActivity.java │ │ │ ├── ScreenUtils.java │ │ │ ├── UnresolvedActivity.java │ │ │ └── common │ │ │ ├── CommonRecyclerAdapter.java │ │ │ ├── CommonRecyclerHolder.java │ │ │ ├── Message.java │ │ │ └── MessageAdapter.java │ └── res │ │ ├── drawable-xxhdpi │ │ ├── icon_file.png │ │ ├── icon_more.png │ │ ├── icon_photo.png │ │ └── icon_voice.png │ │ ├── drawable │ │ └── edit_bg.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_resolved.xml │ │ ├── activity_unresolved.xml │ │ ├── item_message.xml │ │ ├── view_emoji_panel.xml │ │ ├── view_input.xml │ │ └── view_message_list.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── leavesc │ └── hello │ └── keyboard │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── resolved.gif ├── settings.gradle └── unresolved.gif /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Keyboard 2 | 解决软键盘和表情面板切换时的跳闪问题 3 | 4 | 解决前 5 | 6 | ![](https://github.com/leavesC/Keyboard/blob/master/unresolved.gif) 7 | 8 | 9 | 解决后 10 | 11 | ![](https://github.com/leavesC/Keyboard/blob/master/resolved.gif) 12 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "leavesc.hello.keyboard" 7 | minSdkVersion 21 8 | targetSdkVersion 28 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(dir: 'libs', include: ['*.jar']) 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 25 | testImplementation 'junit:junit:4.12' 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 28 | implementation 'com.android.support:recyclerview-v7:28.0.0' 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\Software\SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/leavesc/hello/keyboard/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("leavesc.hello.keyboard", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/EmojiKeyboard.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.content.SharedPreferences; 6 | import android.graphics.Rect; 7 | import android.os.Handler; 8 | import android.util.Log; 9 | import android.view.MotionEvent; 10 | import android.view.View; 11 | import android.view.WindowManager; 12 | import android.view.inputmethod.InputMethodManager; 13 | import android.widget.EditText; 14 | import android.widget.LinearLayout; 15 | 16 | /** 17 | * 作者: chenZY 18 | * 时间: 2017/8/26 18:12 19 | * 描述: 20 | */ 21 | public class EmojiKeyboard { 22 | 23 | private Activity activity; 24 | 25 | //文本输入框 26 | private EditText editText; 27 | 28 | //表情面板 29 | private View emojiPanelView; 30 | 31 | //内容View,即除了表情布局和输入框布局以外的布局 32 | //用于固定输入框一行的高度以防止跳闪 33 | private View contentView; 34 | 35 | private InputMethodManager inputMethodManager; 36 | 37 | private SharedPreferences sharedPreferences; 38 | 39 | private static final String EMOJI_KEYBOARD = "EmojiKeyboard"; 40 | 41 | private static final String KEY_SOFT_KEYBOARD_HEIGHT = "SoftKeyboardHeight"; 42 | 43 | private static final int SOFT_KEYBOARD_HEIGHT_DEFAULT = 654; 44 | 45 | private Handler handler; 46 | 47 | public EmojiKeyboard(Activity activity, EditText editText, View emojiPanelView, View emojiPanelSwitchView, View contentView) { 48 | init(activity, editText, emojiPanelView, emojiPanelSwitchView, contentView); 49 | } 50 | 51 | private void init(Activity activity, EditText editText, View emojiPanelView, View emojiPanelSwitchView, View contentView) { 52 | this.activity = activity; 53 | this.editText = editText; 54 | this.emojiPanelView = emojiPanelView; 55 | this.contentView = contentView; 56 | this.editText.setOnTouchListener(new View.OnTouchListener() { 57 | @Override 58 | public boolean onTouch(final View v, MotionEvent event) { 59 | if (event.getAction() == MotionEvent.ACTION_UP && EmojiKeyboard.this.emojiPanelView.isShown()) { 60 | lockContentViewHeight(); 61 | hideEmojiPanel(true); 62 | unlockContentViewHeight(); 63 | } 64 | return false; 65 | } 66 | }); 67 | this.contentView.setOnTouchListener(new View.OnTouchListener() { 68 | @Override 69 | public boolean onTouch(View view, MotionEvent motionEvent) { 70 | if (motionEvent.getAction() == MotionEvent.ACTION_UP) { 71 | if (EmojiKeyboard.this.emojiPanelView.isShown()) { 72 | hideEmojiPanel(false); 73 | } else if (isSoftKeyboardShown()) { 74 | hideSoftKeyboard(); 75 | } 76 | } 77 | return false; 78 | } 79 | }); 80 | //用于弹出表情面板的View 81 | emojiPanelSwitchView.setOnClickListener(new View.OnClickListener() { 82 | @Override 83 | public void onClick(View v) { 84 | if (EmojiKeyboard.this.emojiPanelView.isShown()) { 85 | lockContentViewHeight(); 86 | hideEmojiPanel(true); 87 | unlockContentViewHeight(); 88 | } else { 89 | if (isSoftKeyboardShown()) { 90 | lockContentViewHeight(); 91 | showEmojiPanel(); 92 | unlockContentViewHeight(); 93 | } else { 94 | showEmojiPanel(); 95 | } 96 | } 97 | } 98 | }); 99 | this.inputMethodManager = (InputMethodManager) this.activity.getSystemService(Context.INPUT_METHOD_SERVICE); 100 | this.sharedPreferences = this.activity.getSharedPreferences(EMOJI_KEYBOARD, Context.MODE_PRIVATE); 101 | this.activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 102 | this.handler = new Handler(); 103 | init(); 104 | } 105 | 106 | /** 107 | * 如果之前没有保存过键盘高度值 108 | * 则在进入Activity时自动打开键盘,并把高度值保存下来 109 | */ 110 | private void init() { 111 | if (!sharedPreferences.contains(KEY_SOFT_KEYBOARD_HEIGHT)) { 112 | handler.postDelayed(new Runnable() { 113 | @Override 114 | public void run() { 115 | showSoftKeyboard(true); 116 | } 117 | }, 200); 118 | } 119 | } 120 | 121 | /** 122 | * 当点击返回键时需要先隐藏表情面板 123 | */ 124 | public boolean interceptBackPress() { 125 | if (emojiPanelView.isShown()) { 126 | hideEmojiPanel(false); 127 | return true; 128 | } 129 | return false; 130 | } 131 | 132 | /** 133 | * 锁定内容View以防止跳闪 134 | */ 135 | private void lockContentViewHeight() { 136 | LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) contentView.getLayoutParams(); 137 | layoutParams.height = contentView.getHeight(); 138 | layoutParams.weight = 0; 139 | } 140 | 141 | /** 142 | * 释放锁定的内容View 143 | */ 144 | private void unlockContentViewHeight() { 145 | handler.postDelayed(new Runnable() { 146 | @Override 147 | public void run() { 148 | ((LinearLayout.LayoutParams) contentView.getLayoutParams()).weight = 1; 149 | } 150 | }, 200); 151 | } 152 | 153 | /** 154 | * 获取键盘的高度 155 | */ 156 | private int getSoftKeyboardHeight() { 157 | Rect rect = new Rect(); 158 | activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); 159 | //屏幕当前可见高度,不包括状态栏 160 | int displayHeight = rect.bottom - rect.top; 161 | //屏幕可用高度 162 | int availableHeight = ScreenUtils.getAvailableScreenHeight(activity); 163 | //用于计算键盘高度 164 | int softInputHeight = availableHeight - displayHeight - ScreenUtils.getStatusBarHeight(activity); 165 | Log.e("TAG-di", displayHeight + ""); 166 | Log.e("TAG-av", availableHeight + ""); 167 | Log.e("TAG-so", softInputHeight + ""); 168 | if (softInputHeight != 0) { 169 | // 因为考虑到用户可能会主动调整键盘高度,所以只能是每次获取到键盘高度时都将其存储起来 170 | sharedPreferences.edit().putInt(KEY_SOFT_KEYBOARD_HEIGHT, softInputHeight).apply(); 171 | } 172 | return softInputHeight; 173 | } 174 | 175 | /** 176 | * 获取本地存储的键盘高度值或者是返回默认值 177 | */ 178 | private int getSoftKeyboardHeightLocalValue() { 179 | return sharedPreferences.getInt(KEY_SOFT_KEYBOARD_HEIGHT, SOFT_KEYBOARD_HEIGHT_DEFAULT); 180 | } 181 | 182 | /** 183 | * 判断是否显示了键盘 184 | */ 185 | private boolean isSoftKeyboardShown() { 186 | return getSoftKeyboardHeight() != 0; 187 | } 188 | 189 | /** 190 | * 令编辑框获取焦点并显示键盘 191 | */ 192 | private void showSoftKeyboard(boolean saveSoftKeyboardHeight) { 193 | editText.requestFocus(); 194 | inputMethodManager.showSoftInput(editText, 0); 195 | if (saveSoftKeyboardHeight) { 196 | handler.postDelayed(new Runnable() { 197 | @Override 198 | public void run() { 199 | getSoftKeyboardHeight(); 200 | } 201 | }, 200); 202 | } 203 | } 204 | 205 | /** 206 | * 隐藏键盘 207 | */ 208 | private void hideSoftKeyboard() { 209 | inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(), 0); 210 | } 211 | 212 | /** 213 | * 显示表情面板 214 | */ 215 | private void showEmojiPanel() { 216 | int softKeyboardHeight = getSoftKeyboardHeight(); 217 | if (softKeyboardHeight == 0) { 218 | softKeyboardHeight = getSoftKeyboardHeightLocalValue(); 219 | } else { 220 | hideSoftKeyboard(); 221 | } 222 | emojiPanelView.getLayoutParams().height = softKeyboardHeight; 223 | emojiPanelView.setVisibility(View.VISIBLE); 224 | if (emojiPanelVisibilityChangeListener != null) { 225 | emojiPanelVisibilityChangeListener.onShowEmojiPanel(); 226 | } 227 | } 228 | 229 | /** 230 | * 隐藏表情面板,同时指定是否随后开启键盘 231 | */ 232 | private void hideEmojiPanel(boolean showSoftKeyboard) { 233 | if (emojiPanelView.isShown()) { 234 | emojiPanelView.setVisibility(View.GONE); 235 | if (showSoftKeyboard) { 236 | showSoftKeyboard(false); 237 | } 238 | if (emojiPanelVisibilityChangeListener != null) { 239 | emojiPanelVisibilityChangeListener.onHideEmojiPanel(); 240 | } 241 | } 242 | } 243 | 244 | public interface OnEmojiPanelVisibilityChangeListener { 245 | 246 | void onShowEmojiPanel(); 247 | 248 | void onHideEmojiPanel(); 249 | } 250 | 251 | private OnEmojiPanelVisibilityChangeListener emojiPanelVisibilityChangeListener; 252 | 253 | public void setEmoticonPanelVisibilityChangeListener(OnEmojiPanelVisibilityChangeListener emojiPanelVisibilityChangeListener) { 254 | this.emojiPanelVisibilityChangeListener = emojiPanelVisibilityChangeListener; 255 | } 256 | 257 | } 258 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/MainActivity.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | public class MainActivity extends AppCompatActivity { 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | setContentView(leavesc.hello.keyboard.R.layout.activity_main); 14 | findViewById(leavesc.hello.keyboard.R.id.btn_unresolved).setOnClickListener(new View.OnClickListener() { 15 | @Override 16 | public void onClick(View v) { 17 | startActivity(new Intent(MainActivity.this, UnresolvedActivity.class)); 18 | } 19 | }); 20 | findViewById(leavesc.hello.keyboard.R.id.btn_resolved).setOnClickListener(new View.OnClickListener() { 21 | @Override 22 | public void onClick(View v) { 23 | startActivity(new Intent(MainActivity.this, ResolvedActivity.class)); 24 | } 25 | }); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/ResolvedActivity.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.Log; 8 | import android.widget.EditText; 9 | import android.widget.ImageView; 10 | import android.widget.LinearLayout; 11 | 12 | import leavesc.hello.keyboard.common.Message; 13 | import leavesc.hello.keyboard.common.MessageAdapter; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | public class ResolvedActivity extends AppCompatActivity { 19 | 20 | private EmojiKeyboard emojiKeyboard; 21 | 22 | private final String TAG = "ResolvedActivity"; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(leavesc.hello.keyboard.R.layout.activity_resolved); 28 | initView(); 29 | } 30 | 31 | private void initView() { 32 | RecyclerView rv_messageList = (RecyclerView) findViewById(leavesc.hello.keyboard.R.id.rv_messageList); 33 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 34 | linearLayoutManager.setStackFromEnd(true); 35 | rv_messageList.setLayoutManager(linearLayoutManager); 36 | List messageList = new ArrayList<>(); 37 | messageList.add(new Message("1")); 38 | messageList.add(new Message("2")); 39 | messageList.add(new Message("3")); 40 | messageList.add(new Message("4")); 41 | messageList.add(new Message("5")); 42 | messageList.add(new Message("6")); 43 | messageList.add(new Message("7")); 44 | messageList.add(new Message("8")); 45 | messageList.add(new Message("9")); 46 | messageList.add(new Message("10")); 47 | messageList.add(new Message("11")); 48 | messageList.add(new Message("12")); 49 | messageList.add(new Message("13")); 50 | messageList.add(new Message("14")); 51 | messageList.add(new Message("15")); 52 | messageList.add(new Message("16")); 53 | messageList.add(new Message("17")); 54 | messageList.add(new Message("18")); 55 | messageList.add(new Message("19")); 56 | messageList.add(new Message("20")); 57 | MessageAdapter messageAdapter = new MessageAdapter(this, messageList, leavesc.hello.keyboard.R.layout.item_message); 58 | rv_messageList.setAdapter(messageAdapter); 59 | 60 | EditText et_inputMessage = (EditText) findViewById(leavesc.hello.keyboard.R.id.et_inputMessage); 61 | ImageView iv_more = (ImageView) findViewById(leavesc.hello.keyboard.R.id.iv_more); 62 | LinearLayout ll_rootEmojiPanel = (LinearLayout) findViewById(leavesc.hello.keyboard.R.id.ll_rootEmojiPanel); 63 | emojiKeyboard = new EmojiKeyboard(this, et_inputMessage, ll_rootEmojiPanel, iv_more, rv_messageList); 64 | emojiKeyboard.setEmoticonPanelVisibilityChangeListener(new EmojiKeyboard.OnEmojiPanelVisibilityChangeListener() { 65 | @Override 66 | public void onShowEmojiPanel() { 67 | Log.e(TAG, "onShowEmojiPanel"); 68 | } 69 | 70 | @Override 71 | public void onHideEmojiPanel() { 72 | Log.e(TAG, "onHideEmojiPanel"); 73 | } 74 | }); 75 | } 76 | 77 | @Override 78 | public void onBackPressed() { 79 | if (!emojiKeyboard.interceptBackPress()) { 80 | super.onBackPressed(); 81 | } 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/ScreenUtils.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard; 2 | 3 | import android.app.Activity; 4 | import android.content.Context; 5 | import android.util.DisplayMetrics; 6 | import android.view.Window; 7 | 8 | /** 9 | * 作者: chenZY 10 | * 时间: 2017/8/26 18:11 11 | * 描述: 12 | */ 13 | public class ScreenUtils { 14 | 15 | /** 16 | * 返回包括虚拟键在内的总的屏幕高度 17 | * 即使虚拟按键显示着,也会加上虚拟按键的高度 18 | */ 19 | public static int getTotalScreenHeight(Activity activity) { 20 | DisplayMetrics displayMetrics = new DisplayMetrics(); 21 | activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics); 22 | return displayMetrics.heightPixels; 23 | } 24 | 25 | /** 26 | * 返回屏幕的宽度 27 | */ 28 | public static int getScreenWidth(Activity activity) { 29 | DisplayMetrics displayMetrics = new DisplayMetrics(); 30 | activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics); 31 | return displayMetrics.widthPixels; 32 | } 33 | 34 | /** 35 | * 返回屏幕可用高度 36 | * 当显示了虚拟按键时,会自动减去虚拟按键高度 37 | */ 38 | public static int getAvailableScreenHeight(Activity activity) { 39 | DisplayMetrics displayMetrics = new DisplayMetrics(); 40 | activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 41 | return displayMetrics.heightPixels; 42 | } 43 | 44 | /** 45 | * 状态栏高度 46 | */ 47 | public static int getStatusBarHeight(Activity activity) { 48 | int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android"); 49 | return activity.getResources().getDimensionPixelSize(resourceId); 50 | } 51 | 52 | /** 53 | * 获取虚拟按键的高度 54 | * 会根据当前是否有显示虚拟按键来返回相应的值 55 | * 即如果隐藏了虚拟按键,则返回零 56 | */ 57 | public static int getVirtualBarHeightIfRoom(Activity activity) { 58 | DisplayMetrics displayMetrics = new DisplayMetrics(); 59 | activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 60 | int usableHeight = displayMetrics.heightPixels; 61 | activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics); 62 | int realHeight = displayMetrics.heightPixels; 63 | return realHeight - usableHeight; 64 | } 65 | 66 | /** 67 | * 获取虚拟按键的高度,不论虚拟按键是否显示都会返回其固定高度 68 | */ 69 | public static int getVirtualBarHeight(Activity activity) { 70 | int resourceId = activity.getResources().getIdentifier("navigation_bar_height", "dimen", "android"); 71 | return activity.getResources().getDimensionPixelSize(resourceId); 72 | } 73 | 74 | /** 75 | * 标题栏高度,如果隐藏了标题栏则返回零 76 | */ 77 | public static int getTitleHeight(Activity activity) { 78 | return activity.getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop(); 79 | } 80 | 81 | /** 82 | * 将dp值转换为px值 83 | */ 84 | public static int dp2px(Context context, float dpValue) { 85 | final float scale = context.getResources().getDisplayMetrics().density; 86 | return (int) (dpValue * scale + 0.5f); 87 | } 88 | 89 | /** 90 | * 将px值转换为dp值 91 | */ 92 | public static int px2dp(Context context, float pxValue) { 93 | float scale = context.getResources().getDisplayMetrics().density; 94 | return (int) (pxValue / scale + 0.5f); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/UnresolvedActivity.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.widget.LinearLayoutManager; 7 | import android.support.v7.widget.RecyclerView; 8 | import android.view.KeyEvent; 9 | import android.view.MotionEvent; 10 | import android.view.View; 11 | import android.view.inputmethod.InputMethodManager; 12 | import android.widget.EditText; 13 | import android.widget.ImageView; 14 | import android.widget.LinearLayout; 15 | 16 | import leavesc.hello.keyboard.common.Message; 17 | import leavesc.hello.keyboard.common.MessageAdapter; 18 | 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | public class UnresolvedActivity extends AppCompatActivity { 23 | 24 | private EditText et_inputMessage; 25 | 26 | private LinearLayout ll_rootEmojiPanel; 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(leavesc.hello.keyboard.R.layout.activity_unresolved); 32 | initView(); 33 | } 34 | 35 | private void initView() { 36 | RecyclerView rv_messageList = (RecyclerView) findViewById(leavesc.hello.keyboard.R.id.rv_messageList); 37 | LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); 38 | linearLayoutManager.setStackFromEnd(true); 39 | rv_messageList.setLayoutManager(linearLayoutManager); 40 | List messageList = new ArrayList<>(); 41 | messageList.add(new Message("1")); 42 | messageList.add(new Message("2")); 43 | messageList.add(new Message("3")); 44 | messageList.add(new Message("4")); 45 | messageList.add(new Message("5")); 46 | messageList.add(new Message("6")); 47 | messageList.add(new Message("7")); 48 | messageList.add(new Message("8")); 49 | messageList.add(new Message("9")); 50 | messageList.add(new Message("10")); 51 | messageList.add(new Message("11")); 52 | messageList.add(new Message("12")); 53 | messageList.add(new Message("13")); 54 | messageList.add(new Message("14")); 55 | messageList.add(new Message("15")); 56 | messageList.add(new Message("16")); 57 | messageList.add(new Message("17")); 58 | messageList.add(new Message("18")); 59 | messageList.add(new Message("19")); 60 | messageList.add(new Message("20")); 61 | MessageAdapter messageAdapter = new MessageAdapter(this, messageList, leavesc.hello.keyboard.R.layout.item_message); 62 | rv_messageList.setAdapter(messageAdapter); 63 | 64 | et_inputMessage = (EditText) findViewById(leavesc.hello.keyboard.R.id.et_inputMessage); 65 | ImageView iv_more = (ImageView) findViewById(leavesc.hello.keyboard.R.id.iv_more); 66 | ll_rootEmojiPanel = (LinearLayout) findViewById(leavesc.hello.keyboard.R.id.ll_rootEmojiPanel); 67 | iv_more.setOnClickListener(new View.OnClickListener() { 68 | @Override 69 | public void onClick(View v) { 70 | if (ll_rootEmojiPanel.getVisibility() == View.VISIBLE) { 71 | showKeyboard(); 72 | } else { 73 | hideKeyboard(); 74 | ll_rootEmojiPanel.setVisibility(View.VISIBLE); 75 | } 76 | } 77 | }); 78 | et_inputMessage.setOnFocusChangeListener(new View.OnFocusChangeListener() { 79 | @Override 80 | public void onFocusChange(View view, boolean b) { 81 | if (b) { 82 | ll_rootEmojiPanel.setVisibility(View.GONE); 83 | } 84 | } 85 | }); 86 | rv_messageList.setOnTouchListener(new View.OnTouchListener() { 87 | @Override 88 | public boolean onTouch(View view, MotionEvent motionEvent) { 89 | if (motionEvent.getAction() == MotionEvent.ACTION_UP) { 90 | hideKeyboard(); 91 | ll_rootEmojiPanel.setVisibility(View.GONE); 92 | } 93 | return false; 94 | } 95 | }); 96 | } 97 | 98 | private void showKeyboard() { 99 | et_inputMessage.requestFocus(); 100 | InputMethodManager inputMethodManager = (InputMethodManager) et_inputMessage.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 101 | inputMethodManager.showSoftInput(et_inputMessage, 0); 102 | } 103 | 104 | private void hideKeyboard() { 105 | InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 106 | et_inputMessage.clearFocus(); 107 | inputMethodManager.hideSoftInputFromWindow(et_inputMessage.getWindowToken(), 0); 108 | } 109 | 110 | @Override 111 | public boolean dispatchKeyEvent(KeyEvent event) { 112 | // if (event.getAction() == KeyEvent.ACTION_UP && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { 113 | // if (ll_rootEmojiPanel.getVisibility() == View.VISIBLE) { 114 | // ll_rootEmojiPanel.setVisibility(View.GONE); 115 | // return true; 116 | // } 117 | // } 118 | return super.dispatchKeyEvent(event); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/common/CommonRecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard.common; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.LayoutRes; 5 | import android.support.v7.widget.RecyclerView; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import java.util.List; 11 | 12 | /** 13 | * 通用RecyclerView Adapter 14 | * Created by ZY on 2017/6/3. 15 | */ 16 | public abstract class CommonRecyclerAdapter extends RecyclerView.Adapter { 17 | 18 | //多布局支持 19 | public interface MultiTypeSupport { 20 | 21 | int getLayoutId(T item, int position); 22 | 23 | } 24 | 25 | private MultiTypeSupport multiTypeSupport; 26 | 27 | private LayoutInflater layoutInflater; 28 | 29 | private List dataList; 30 | 31 | @LayoutRes 32 | private int layoutId; 33 | 34 | private CommonRecyclerHolder.onClickCommonListener clickCommonListener; 35 | 36 | /** 37 | * 私有构造函数 38 | * 39 | * @param context 上下文 40 | * @param dataList 数据集合 41 | */ 42 | private CommonRecyclerAdapter(Context context, List dataList) { 43 | this.layoutInflater = LayoutInflater.from(context); 44 | this.dataList = dataList; 45 | } 46 | 47 | /** 48 | * 适用于:列表所有的子项都使用相同的布局文件,且不需要监听点击事件 49 | * 50 | * @param context 上下文 51 | * @param dataList 数据集合 52 | * @param layoutId 布局文件ID 53 | */ 54 | protected CommonRecyclerAdapter(Context context, List dataList, @LayoutRes int layoutId) { 55 | this(context, dataList); 56 | this.layoutId = layoutId; 57 | } 58 | 59 | /** 60 | * 适用于:列表的子项使用不同的布局文件,且不需要监听点击事件 61 | * 62 | * @param context 上下文 63 | * @param dataList 数据集合 64 | * @param multiTypeSupport 支持多个布局文件 65 | */ 66 | protected CommonRecyclerAdapter(Context context, List dataList, MultiTypeSupport multiTypeSupport) { 67 | this(context, dataList); 68 | this.multiTypeSupport = multiTypeSupport; 69 | } 70 | 71 | /** 72 | * 适用于:列表所有的子项都使用相同的布局文件,且需要监听点击事件 73 | * 74 | * @param context 上下文 75 | * @param dataList 数据集合 76 | * @param layoutId 布局文件ID 77 | * @param clickCommonListener 点击事件监听 78 | */ 79 | protected CommonRecyclerAdapter(Context context, List dataList, @LayoutRes int layoutId, 80 | CommonRecyclerHolder.onClickCommonListener clickCommonListener) { 81 | this(context, dataList, layoutId); 82 | this.clickCommonListener = clickCommonListener; 83 | } 84 | 85 | /** 86 | * 适用于:列表的子项使用不同的布局文件,且需要监听点击事件 87 | * 88 | * @param context 上下文 89 | * @param dataList 数据集合 90 | * @param multiTypeSupport 支持多个布局文件 91 | * @param clickCommonListener 点击事件监听 92 | */ 93 | protected CommonRecyclerAdapter(Context context, List dataList, MultiTypeSupport multiTypeSupport, 94 | CommonRecyclerHolder.onClickCommonListener clickCommonListener) { 95 | this(context, dataList, multiTypeSupport); 96 | this.clickCommonListener = clickCommonListener; 97 | } 98 | 99 | @Override 100 | public int getItemViewType(int position) { 101 | if (multiTypeSupport != null) { 102 | return multiTypeSupport.getLayoutId(dataList.get(position), position); 103 | } 104 | return super.getItemViewType(position); 105 | } 106 | 107 | @Override 108 | public CommonRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { 109 | if (multiTypeSupport != null) { 110 | layoutId = viewType; 111 | } 112 | View view = layoutInflater.inflate(layoutId, parent, false); 113 | return new CommonRecyclerHolder(view); 114 | } 115 | 116 | @Override 117 | public void onBindViewHolder(CommonRecyclerHolder holder, int position) { 118 | bindData(holder, dataList.get(position)); 119 | holder.setClickCommonListener(clickCommonListener); 120 | } 121 | 122 | @Override 123 | public int getItemCount() { 124 | return dataList.size(); 125 | } 126 | 127 | protected abstract void bindData(CommonRecyclerHolder holder, T data); 128 | 129 | } 130 | 131 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/common/CommonRecyclerHolder.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard.common; 2 | 3 | import android.graphics.Bitmap; 4 | import android.support.annotation.DrawableRes; 5 | import android.support.annotation.IdRes; 6 | import android.support.v7.widget.RecyclerView; 7 | import android.util.SparseArray; 8 | import android.view.View; 9 | import android.widget.ImageView; 10 | import android.widget.TextView; 11 | 12 | /** 13 | * 通用ViewHolder 14 | * Created by ZY on 2017/6/3. 15 | */ 16 | public class CommonRecyclerHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 17 | 18 | public interface onClickCommonListener { 19 | 20 | void onClick(int position); 21 | 22 | void onLongClick(int position); 23 | 24 | } 25 | 26 | private onClickCommonListener clickCommonListener; 27 | 28 | //用来存放View以减少findViewById的次数 29 | private SparseArray viewSparseArray; 30 | 31 | CommonRecyclerHolder(View itemView) { 32 | super(itemView); 33 | viewSparseArray = new SparseArray<>(); 34 | itemView.setOnClickListener(this); 35 | itemView.setOnLongClickListener(this); 36 | } 37 | 38 | void setClickCommonListener(onClickCommonListener clickCommonListener) { 39 | this.clickCommonListener = clickCommonListener; 40 | } 41 | 42 | @Override 43 | public void onClick(View view) { 44 | if (clickCommonListener != null) { 45 | clickCommonListener.onClick(getAdapterPosition()); 46 | } 47 | } 48 | 49 | @Override 50 | public boolean onLongClick(View view) { 51 | if (clickCommonListener != null) { 52 | clickCommonListener.onLongClick(getAdapterPosition()); 53 | } 54 | return true; 55 | } 56 | 57 | /** 58 | * 根据 ID 来获取 View 59 | * 60 | * @param viewId viewID 61 | * @param 泛型 62 | * @return 将结果强转为 View 或 View 的子类型 63 | */ 64 | private T getView(@IdRes int viewId) { 65 | // 先从缓存中找,找到的话则直接返回 66 | // 如果找不到则findViewById,再把结果存入缓存中 67 | View view = viewSparseArray.get(viewId); 68 | if (view == null) { 69 | view = itemView.findViewById(viewId); 70 | viewSparseArray.put(viewId, view); 71 | } 72 | return (T) view; 73 | } 74 | 75 | public CommonRecyclerHolder setText(@IdRes int viewId, CharSequence text) { 76 | TextView tv = getView(viewId); 77 | tv.setText(text); 78 | return this; 79 | } 80 | 81 | public CommonRecyclerHolder setImageResource(@IdRes int viewId, @DrawableRes int resourceId) { 82 | ImageView imageView = getView(viewId); 83 | imageView.setImageResource(resourceId); 84 | return this; 85 | } 86 | 87 | public CommonRecyclerHolder setImageResource(@IdRes int viewId, Bitmap bitmap) { 88 | ImageView imageView = getView(viewId); 89 | imageView.setImageBitmap(bitmap); 90 | return this; 91 | } 92 | 93 | public CommonRecyclerHolder setViewVisibility(@IdRes int viewId, int visibility) { 94 | getView(viewId).setVisibility(visibility); 95 | return this; 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/common/Message.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard.common; 2 | 3 | /** 4 | * 作者:叶应是叶 5 | * 时间:2017/8/27 14:17 6 | * 描述: 7 | */ 8 | public class Message { 9 | 10 | private String message; 11 | 12 | public Message(String message) { 13 | this.message = message; 14 | } 15 | 16 | public String getMessage() { 17 | return message; 18 | } 19 | 20 | public void setMessage(String message) { 21 | this.message = message; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/leavesc/hello/keyboard/common/MessageAdapter.java: -------------------------------------------------------------------------------- 1 | package leavesc.hello.keyboard.common; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.LayoutRes; 5 | 6 | import java.util.List; 7 | 8 | import leavesc.hello.keyboard.R; 9 | 10 | /** 11 | * 作者:叶应是叶 12 | * 时间:2017/8/27 14:17 13 | * 描述: 14 | */ 15 | public class MessageAdapter extends CommonRecyclerAdapter { 16 | 17 | public MessageAdapter(Context context, List messageList, @LayoutRes int layoutId) { 18 | super(context, messageList, layoutId); 19 | } 20 | 21 | @Override 22 | protected void bindData(CommonRecyclerHolder holder, Message message) { 23 | holder.setText(R.id.tv_message, message.getMessage()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_file.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leavesCZY/Keyboard/4a7492a00d5452a563f2ba4db5a6a9ba91e94846/app/src/main/res/drawable-xxhdpi/icon_file.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_more.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leavesCZY/Keyboard/4a7492a00d5452a563f2ba4db5a6a9ba91e94846/app/src/main/res/drawable-xxhdpi/icon_more.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_photo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leavesCZY/Keyboard/4a7492a00d5452a563f2ba4db5a6a9ba91e94846/app/src/main/res/drawable-xxhdpi/icon_photo.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/icon_voice.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leavesCZY/Keyboard/4a7492a00d5452a563f2ba4db5a6a9ba91e94846/app/src/main/res/drawable-xxhdpi/icon_voice.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/edit_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 |