KeyBoardDialogUtils.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package me.yoqi.android.safekeyboard.keyboard;
  2. import android.app.Activity;
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.os.Build;
  7. import android.text.InputType;
  8. import android.view.Gravity;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. import android.view.inputmethod.InputMethodManager;
  15. import android.widget.EditText;
  16. import java.lang.reflect.Method;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import me.yoqi.android.safekeyboard.R;
  20. public class KeyBoardDialogUtils implements View.OnClickListener {
  21. protected View view;
  22. protected Dialog popWindow;
  23. protected Activity mContext;
  24. private EditText contentView;
  25. private List<String> contentList;
  26. private KhKeyboardView keyboardUtil;
  27. /**
  28. * 构造方法
  29. *
  30. * @param mContext 全局 context 对象
  31. */
  32. public KeyBoardDialogUtils(Activity mContext) {
  33. try {
  34. this.mContext = mContext;
  35. if (contentList == null) {
  36. contentList = new ArrayList<>();
  37. }
  38. if (popWindow == null) {
  39. view = LayoutInflater.from(mContext).inflate(R.layout.keyboard_key_board_popu, null);
  40. // view.getBackground().setAlpha(68);
  41. popWindow = new Dialog(mContext, R.style.keyboard_popupAnimation);
  42. view.findViewById(R.id.keyboard_finish).setOnClickListener(this);
  43. view.findViewById(R.id.keyboard_back_hide).setOnClickListener(this);
  44. }
  45. popWindow.setContentView(view);
  46. popWindow.setCanceledOnTouchOutside(true);
  47. Window mWindow = popWindow.getWindow();
  48. mWindow.setWindowAnimations(R.style.keyboard_popupAnimation);
  49. mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  50. mWindow.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL);
  51. mWindow.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
  52. ViewGroup.LayoutParams.WRAP_CONTENT);
  53. popWindow.setOnDismissListener(new DialogInterface.OnDismissListener() {
  54. @Override
  55. public void onDismiss(DialogInterface dialog) {
  56. if (contentView != null && contentView.isFocused()) {
  57. contentView.clearFocus();
  58. }
  59. }
  60. });
  61. initView();
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. }
  66. private void initView() {
  67. try {
  68. if (keyboardUtil == null)
  69. keyboardUtil = new KhKeyboardView(mContext, view);
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. /**
  75. * 隐藏系统键盘
  76. *
  77. * @param editText
  78. */
  79. public void hideSystemSofeKeyboard(EditText editText) {
  80. int sdkInt = Build.VERSION.SDK_INT;
  81. if (sdkInt >= 11) {
  82. try {
  83. Class<EditText> cls = EditText.class;
  84. Method setShowSoftInputOnFocus;
  85. setShowSoftInputOnFocus = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
  86. setShowSoftInputOnFocus.setAccessible(true);
  87. setShowSoftInputOnFocus.invoke(editText, false);
  88. } catch (SecurityException e) {
  89. e.printStackTrace();
  90. } catch (NoSuchMethodException e) {
  91. e.printStackTrace();
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. } else {
  96. editText.setInputType(InputType.TYPE_NULL);
  97. }
  98. // 如果软键盘已经显示,则隐藏
  99. InputMethodManager imm = (InputMethodManager) mContext.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
  100. imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
  101. }
  102. /**
  103. * 显示键盘界面
  104. *
  105. * @param editText 输入框
  106. */
  107. public void show(final EditText editText) {
  108. editText.setFocusable(true);
  109. editText.setFocusableInTouchMode(true);
  110. editText.requestFocus();
  111. hideSystemSofeKeyboard(editText); //隐藏系统键盘不然系统键盘会闪一下
  112. popWindow.show();
  113. keyboardUtil.showKeyboard(editText);
  114. }
  115. public void dismiss() {
  116. if (popWindow != null && popWindow.isShowing()) {
  117. popWindow.dismiss();
  118. }
  119. }
  120. @Override
  121. public void onClick(View v) {
  122. try {
  123. int i = v.getId();
  124. if (i == R.id.keyboard_finish) {
  125. keyboardUtil.hideKeyboard();
  126. dismiss();
  127. } else if (i == R.id.keyboard_back_hide) {
  128. keyboardUtil.hideKeyboard();
  129. dismiss();
  130. }
  131. } catch (Exception e) {
  132. e.printStackTrace();
  133. }
  134. }
  135. }