下面列出了怎么用android.view.inputmethod.InputMethodManager的API类实例代码及写法,或者点击链接到github查看源代码。
private void closeSearch() {
this.materialMenu.animateState(IconState.BURGER);
this.logo.setVisibility(View.VISIBLE);
this.drawerLogo.setVisibility(View.VISIBLE);
this.search.setVisibility(View.GONE);
this.results.setVisibility(View.GONE);
if (tint != null && rootLayout != null) {
rootLayout.removeView(tint);
}
if (listener != null)
listener.onSearchClosed();
micStateChanged(true);
mic.setImageDrawable(context.getResources().getDrawable(
R.drawable.ic_action_mic));
InputMethodManager inputMethodManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getApplicationWindowToken(),
0);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.es_layout);
inputMethodManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
et_file = (EditText) findViewById(R.id.es_et_file);
et_error = (EditText) findViewById(R.id.es_et_error);
b_read = (Button) findViewById(R.id.es_b_read);
tv_output = (TextView) findViewById(R.id.es_tv_output);
et_file.setText(DEFAULT_FILE);
b_read.setOnClickListener(this);
}
private void handleRestore(@NonNull Context context, @NonNull BackupUtil.BackupInfo backup) {
View view = LayoutInflater.from(context).inflate(R.layout.enter_backup_passphrase_dialog, null);
EditText prompt = view.findViewById(R.id.restore_passphrase_input);
prompt.addTextChangedListener(new PassphraseAsYouTypeFormatter());
new AlertDialog.Builder(context)
.setTitle(R.string.RegistrationActivity_enter_backup_passphrase)
.setView(view)
.setPositiveButton(R.string.RegistrationActivity_restore, (dialog, which) -> {
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(prompt.getWindowToken(), 0);
setSpinning(restoreButton);
skipRestoreButton.setVisibility(View.INVISIBLE);
String passphrase = prompt.getText().toString();
restoreAsynchronously(context, backup, passphrase);
})
.setNegativeButton(android.R.string.cancel, null)
.show();
Log.i(TAG, "Prompt for backup passphrase shown to user.");
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (getView() == null || actionId != EditorInfo.IME_ACTION_DONE) {
return false;
}
switch (v.getId()) {
case R.id.service_name:
regTypeEditText.requestFocus();
return true;
case R.id.reg_type:
portEditText.requestFocus();
return true;
case R.id.port:
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
return true;
}
return false;
}
static public void restartApp(Context context, String packageName)
{
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
am.killBackgroundProcesses(packageName);
Intent it = context.getPackageManager().getLaunchIntentForPackage(packageName);
Activity a = (Activity) context;
if (it != null)
{
if (a.getCurrentFocus() != null)
{
((InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(a.getCurrentFocus().getWindowToken(), 0);
}
it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
}
else
{
Toast.makeText(context, context.getString(R.string.msg_app_launch_fail), Toast.LENGTH_SHORT).show();
}
}
private void postComment()
{
if (sendEditText.getText().toString().length() > 0)
{
Bundle params = new Bundle();
params.putString("message", sendEditText.getText().toString());
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(sendEditText.getWindowToken(), 0);
sendRequest(params);
}
else
{
AlertUtil.showAlert(getActivity(), R.string.error, R.string.define_comment_before_publish, R.string.ok);
}
}
/**
* Navigate to the new page
*
* @param url to load
*/
private void navigate(final String url) {
InputMethodManager imm = (InputMethodManager)this.cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
this.cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
if (!url.startsWith("http") && !url.startsWith("file:")) {
InAppBrowser.this.inAppWebView.loadUrl("http://" + url);
} else {
InAppBrowser.this.inAppWebView.loadUrl(url);
}
InAppBrowser.this.inAppWebView.requestFocus();
}
});
}
public static void hideKeyboard(View view)
{
if (view == null)
{
return;
}
try
{
InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (!imm.isActive())
{
return;
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
catch (Exception e)
{
FileLog.e(e);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//NFC
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
mInputMethodManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
mHandler = new Handler();
mUnlockDialog = buildUnlockDialog();
// Register observer to detect if app goes to background
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
// Set wallet fragment as beginning fragment
mFt = getSupportFragmentManager().beginTransaction();
mCurrentFragment = new WalletFragment();
mFt.replace(R.id.mainContent, mCurrentFragment);
mFt.commit();
// Setup Listener
BottomNavigationView navigation = findViewById(R.id.mainNavigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
//http://stackoverflow.com/questions/4341600/how-to-prevent-multiple-instances-of-an-activity-when-it-is-launched-with-differ/
//理论上应该放在launcher activity,放在基类中所有集成此库的app都可以避免此问题
if(!isTaskRoot()){
Intent intent = getIntent();
String action = intent.getAction();
if(intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)){
finish();
return;
}
}
inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
/**
* 初始化控件
*/
private void initialView() {
initViewPager();
DialogView.Initial(this, "正在评论......");
imm = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
mPager = (ViewPager) findViewById(R.id.news_content_vPager);
mPager.setOnPageChangeListener(this);
et_pinglun = (EditText) findViewById(R.id.news_content_editText_pinglun);
tv_pinglun = (TextView) findViewById(R.id.news_content_text_showpinglun);
tv_uppinglun = (TextView) findViewById(R.id.news_content_text_enterpinglun);
tv_pinglun.setText(content.getCpinglun() + "评");
back = (LinearLayout) findViewById(R.id.news_content_back);
back.setOnClickListener(this);
tv_pinglun.setOnClickListener(this);
tv_uppinglun.setOnClickListener(this);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(0);
}
public ActivityUtils setSoftKeyboardVisibile(boolean visible, View... editView) {
final Activity activity = _activity;
if (activity != null) {
final View v = (editView != null && editView.length > 0) ? (editView[0]) : (activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null ? activity.getCurrentFocus() : null);
final InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (v != null && imm != null) {
Runnable r = () -> {
if (visible) {
v.requestFocus();
imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
} else {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
};
r.run();
for (int d : new int[]{100, 350}) {
v.postDelayed(r, d);
}
}
}
return this;
}
protected void showKeyboard() {
Activity activity = getActivity();
if (activity == null) {
return;
}
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return;
}
if (activity.getCurrentFocus() == null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
} else {
imm.showSoftInput(activity.getCurrentFocus(), 0);
}
}
/**
* {@inheritDoc}
*/
@MainThread
@Override
public void hideSoftInput(int flags, ResultReceiver resultReceiver) {
if (DEBUG) Log.v(TAG, "hideSoftInput()");
boolean wasVis = isInputViewShown();
mShowInputFlags = 0;
mShowInputRequested = false;
doHideWindow();
clearInsetOfPreviousIme();
if (resultReceiver != null) {
resultReceiver.send(wasVis != isInputViewShown()
? InputMethodManager.RESULT_HIDDEN
: (wasVis ? InputMethodManager.RESULT_UNCHANGED_SHOWN
: InputMethodManager.RESULT_UNCHANGED_HIDDEN), null);
}
}
@Override
public void showSoftKeyboard(final boolean show) {
view.getHandler().post(new Runnable() {
@Override
public void run() {
InputMethodManager manager =
(InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (show) {
manager.showSoftInput(view, 0);
} else {
manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
});
}
/**
* Hides the soft keyboard from screen
*
* @param view Usually the EditText, but in dynamically layouts you should pass the layout
* instead of the EditText
* @return true, if keyboard has been hidden, otherwise false (i.e. the keyboard was not displayed
* on the screen or no Softkeyboard because device has hardware keyboard)
*/
public static boolean hideKeyboard(View view) {
if (view == null) {
throw new NullPointerException("View is null!");
}
try {
InputMethodManager imm =
(InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return false;
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Exception e) {
return false;
}
return true;
}
private void closeFragment() {
((MainActivity) getActivity()).showNavigationNoFab();
getActivity()
.getSupportFragmentManager()
.beginTransaction()
.remove(EditPoiFragment.this)
.commit();
//Close keyboard
// Check if no view has focus:
View view = getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
pressSearch = (keyCode == KeyEvent.KEYCODE_ENTER);
if (pressSearch && listener != null) {
/*隐藏软键盘*/
InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
}
if (event.getAction() == KeyEvent.ACTION_UP) {
pressSearch = false;
listener.onSearchClick(v);
}
}
return false;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container, false);
ButterKnife.inject(this,view);
USERNAME = getString(R.string.login_username);
PASSWORD = getString(R.string.login_password);
FAIL_MESSAGE = getString(R.string.login_fail_message);
SUCCESS_MESSAGE = getString(R.string.login_success_message);
ALT_BUTTON_FAIL_TITLE = getString(R.string.try_login_again_button_title);
ALT_BUTTON_SUCCESS_TITLE = getString(R.string.logout_button_title);
imm = (InputMethodManager)getActivity().getSystemService(getActivity().getApplicationContext().INPUT_METHOD_SERVICE);
return view;
}
/**
* Hides the soft keyboard.
*
* @param activity The reference of the activity displaying the keyboard.
*/
public static void hide(@NonNull final Activity activity) {
final InputMethodManager iManager = (InputMethodManager) activity.
getSystemService(Context.INPUT_METHOD_SERVICE);
final View view = activity.getCurrentFocus();
if (view != null && iManager != null) {
iManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
/**
* Shows soft keyboard and requests focus for given view.
*/
public static void showSoftKeyboard(Context context, View view) {
if (view == null) {
return;
}
final InputMethodManager manager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
view.requestFocus();
manager.showSoftInput(view, 0);
}
public static void hideSoftInput(Activity activity) {
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
/**
* 隐藏软键盘
*/
public void hideKeyboard() {
if (activity.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
if (activity.getCurrentFocus() != null)
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* Go away keyboard, nobody likes you.
*
* @param context The current Context or Activity that this method is called from.
* @param field field that holds the keyboard focus.
*/
public static void closeKeyboard(Context context, View field) {
try {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(field.getWindowToken(), 0);
} catch (Exception ex) {
Log.e("Caffeine", "Error occurred trying to hide the keyboard. Exception=" + ex);
}
}
@Override
void closeInputMethod() {
Activity activity = getSource();
View focusView = activity.getCurrentFocus();
if (focusView != null) {
InputMethodManager manager = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (manager != null) {
manager.hideSoftInputFromWindow(focusView.getWindowToken(), 0);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
final boolean ret = super.onTouchEvent(event);
// Must be done after super.onTouchEvent()
final InputMethodManager imm = ((InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE));
if (imm != null && imm.isActive(this))
imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
return ret;
}
/**
* 为给定的编辑器开启软键盘
*
* @param editText 给定的编辑器
*/
public static void openSoftKeyboard(Context context, EditText editText) {
editText.requestFocus();
InputMethodManager inputMethodManager
= (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText,
InputMethodManager.SHOW_IMPLICIT);
}
/**
* Show keyboard
* @param context context
* @param view The currently focused view, which would like to receive
* soft keyboard input.
*/
public static void showSoftInput(Context context, View view) {
if (view == null)
return;
InputMethodManager inputMethodManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, 0);
}
public static void toggleSoftInput(Context context, View view) {
if (view.requestFocus()) {
InputMethodManager imm = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, 0);
}
}
/**
* 隐藏软键盘
*/
public static void showKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.
getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
}
}