kongzue 3 роки тому
батько
коміт
f64e8ca4c9

+ 4 - 8
DialogX/src/main/java/com/kongzue/dialogx/dialogs/MessageDialog.java

@@ -346,8 +346,7 @@ public class MessageDialog extends BaseDialog {
                                 if (txtInput == null) return;
                                 txtInput.requestFocus();
                                 txtInput.setFocusableInTouchMode(true);
-                                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
-                                imm.showSoftInput(txtInput, InputMethodManager.RESULT_UNCHANGED_SHOWN);
+                                imeShow(txtInput,true);
                                 txtInput.setSelection(txtInput.getText().length());
                                 if (inputInfo != null && inputInfo.isSelectAllText()) {
                                     txtInput.selectAll();
@@ -387,8 +386,7 @@ public class MessageDialog extends BaseDialog {
                 @Override
                 public void onClick(View v) {
                     if (txtInput != null) {
-                        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
-                        imm.hideSoftInputFromWindow(txtInput.getWindowToken(), 0);
+                        imeShow(txtInput,false);
                     }
                     if (okButtonClickListener != null) {
                         if (okButtonClickListener instanceof OnInputDialogButtonClickListener) {
@@ -410,8 +408,7 @@ public class MessageDialog extends BaseDialog {
                 @Override
                 public void onClick(View v) {
                     if (txtInput != null) {
-                        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
-                        imm.hideSoftInputFromWindow(txtInput.getWindowToken(), 0);
+                        imeShow(txtInput,false);
                     }
                     if (cancelButtonClickListener != null) {
                         if (cancelButtonClickListener instanceof OnInputDialogButtonClickListener) {
@@ -433,8 +430,7 @@ public class MessageDialog extends BaseDialog {
                 @Override
                 public void onClick(View v) {
                     if (txtInput != null) {
-                        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
-                        imm.hideSoftInputFromWindow(txtInput.getWindowToken(), 0);
+                        imeShow(txtInput,false);
                     }
                     if (otherButtonClickListener != null) {
                         if (otherButtonClickListener instanceof OnInputDialogButtonClickListener) {

+ 14 - 2
DialogX/src/main/java/com/kongzue/dialogx/interfaces/BaseDialog.java

@@ -16,6 +16,8 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.EditText;
 import android.widget.FrameLayout;
 import android.widget.LinearLayout;
 import android.widget.TextView;
@@ -105,7 +107,7 @@ public abstract class BaseDialog {
                 runOnMain(new Runnable() {
                     @Override
                     public void run() {
-                        if (view.getParent() == rootFrameLayout.get()){
+                        if (view.getParent() == rootFrameLayout.get()) {
                             error(((BaseDialog) view.getTag()).dialogKey() + "已处于显示状态,请勿重复执行 show() 指令。");
                             return;
                         }
@@ -151,7 +153,7 @@ public abstract class BaseDialog {
                 runOnMain(new Runnable() {
                     @Override
                     public void run() {
-                        if (view.getParent() == rootFrameLayout.get()){
+                        if (view.getParent() == rootFrameLayout.get()) {
                             error(((BaseDialog) view.getTag()).dialogKey() + "已处于显示状态,请勿重复执行 show() 指令。");
                             return;
                         }
@@ -424,4 +426,14 @@ public abstract class BaseDialog {
     public static List<BaseDialog> getRunningDialogList() {
         return new CopyOnWriteArrayList<>(runningDialogList);
     }
+    
+    protected void imeShow(EditText editText, boolean show) {
+        if (getContext() == null) return;
+        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+        if (show) {
+            imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
+        } else {
+            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
+        }
+    }
 }