Improved show/hide keyboard

This commit is contained in:
M66B
2021-04-27 16:40:27 +02:00
parent ee12d06469
commit 62cc21464b
4 changed files with 47 additions and 30 deletions

View File

@@ -64,6 +64,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.MimeTypeMap;
import android.webkit.WebView;
import android.widget.Button;
@@ -923,6 +924,43 @@ public class Helper {
return color;
}
static void showKeyboard(final View view) {
final InputMethodManager imm =
(InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null)
return;
if (view.hasFocus())
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
final View.OnFocusChangeListener listener = view.getOnFocusChangeListener();
view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (listener != null)
listener.onFocusChange(v, hasFocus);
if (hasFocus)
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
else
imm.toggleSoftInput(0, 0);
}
});
if (!view.hasFocus())
view.requestFocus();
}
static void hideKeyboard(final View view) {
InputMethodManager imm =
(InputMethodManager) view.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm == null)
return;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
// Formatting
private static final DecimalFormat df = new DecimalFormat("@@");