下面列出了android.view.inputmethod.InputMethodManager#toggleSoftInput ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onBackPressed() {
if (smiley.isShown()) {
removeViewWithAnim(smiley);
} else if (!TextUtils.isEmpty(et.getText().toString()) && canShowSaveDraftDialog()) {
SaveDraftDialog dialog = new SaveDraftDialog();
dialog.show(getFragmentManager(), "");
} else {
if (BeeboApplication.getInstance().getAccountBean().equals(getCurrentAccountBean())) {
super.onBackPressed();
} else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
}
Intent intent = MainTimeLineActivity.newIntent(getCurrentAccountBean());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
@Override
protected void onStart() {
super.onStart();
simpleSearchView.showSearch(false);
simpleSearchView.getSearchEditText().requestFocus();
if (query != null) {
simpleSearchView.getSearchEditText().setText(query);
simpleSearchView.getSearchEditText().setSelection(query.length());
query = null;
}
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
protected void showKeyboard(boolean isShow) {
Activity activity = getActivity();
if (activity == null) {
return;
}
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null) {
return;
}
if (isShow) {
if (activity.getCurrentFocus() == null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
} else {
imm.showSoftInput(activity.getCurrentFocus(), 0);
}
} else {
if (activity.getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
/**
* 切换键盘显示与否状态
*/
public static void toggleSoftInput(Context context, EditText edit) {
edit.setFocusable(true);
edit.setFocusableInTouchMode(true);
edit.requestFocus();
InputMethodManager inputManager = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
@Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
boolean focus = super.requestFocus(direction, previouslyFocusedRect);
if (currentTextEntity != null && focus) {
currentTextEntity.setFocused(true);
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT);
if (!imm.isAcceptingText()) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}
return focus;
}
/**
* EditText获取焦点并显示软键盘
*/
//弹出软键盘
public static void showKeyboard(EditText editText, Context context) {
//其中editText为dialog中的输入框的 EditText
if (editText != null) {
//设置可获得焦点
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
//请求获得焦点
editText.requestFocus();
//调用系统输入法
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* 通知监听者 进行搜索操作
* @param text
*/
private void notifyStartSearching(String text){
if (mListener != null) {
mListener.onSearch(etInput.getText().toString());
}
//隐藏软键盘
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 打开软键盘
* @param editText {@link EditText}
* @return {@code true} success, {@code false} fail
*/
public static boolean openKeyboard(final EditText editText) {
if (editText != null) {
try {
InputMethodManager imm = AppUtils.getInputMethodManager();
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
return true;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "openKeyboard");
}
}
return false;
}
protected void showKeyboard(boolean isShow) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (isShow) {
if (getCurrentFocus() == null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
} else {
imm.showSoftInput(getCurrentFocus(), 0);
}
} else {
if (getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
/**
* If the soft keyboard is open, this method will close it. Currently only
* works for API 16 and above.
*/
private void closeKeyboardIfOpen()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
final View activityRootView = getWindow().getDecorView().getRootView();
final OnGlobalLayoutListener globalListener = new OnGlobalLayoutListener()
{
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onGlobalLayout()
{
Rect rect = new Rect();
// rect will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(rect);
int heightDiff = activityRootView.getRootView().getHeight() - (rect.bottom - rect.top);
if (heightDiff > 100)
{
// If the difference is more than 100 pixels, it's probably caused by the soft keyboard being open. Now we want to close it.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); // Toggle the soft keyboard.
}
// Now we have to remove the OnGlobalLayoutListener, otherwise we will experience errors
activityRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
};
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(globalListener);
}
}
/**
* Toggle keyboard If the keyboard is visible,then hidden it,if it's
* invisible,then show it
*
* @param context 上下文
*/
public static void toggleKeyboard(Context context) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) {
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
InputMethodManager.HIDE_NOT_ALWAYS);
}
}
/**
* If the soft keyboard is open, this method will close it. Currently only
* works for API 16 and above.
*/
private void closeKeyboardIfOpen()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
final View activityRootView = getWindow().getDecorView().getRootView();
final OnGlobalLayoutListener globalListener = new OnGlobalLayoutListener()
{
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onGlobalLayout()
{
Rect rect = new Rect();
// rect will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(rect);
int heightDiff = activityRootView.getRootView().getHeight() - (rect.bottom - rect.top);
if (heightDiff > 100)
{
// If the difference is more than 100 pixels, it's probably caused by the soft keyboard being open. Now we want to close it.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); // Toggle the soft keyboard.
}
// Now we have to remove the OnGlobalLayoutListener, otherwise we will experience errors
activityRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
};
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(globalListener);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
((Infinity) getApplication()).getAppComponent().inject(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_post);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
applyCustomTheme();
if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK_FROM_POST_DETAIL, true)) {
Slidr.attach(this);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) {
addOnOffsetChangedListener(appBarLayout);
}
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mFullName = getIntent().getStringExtra(EXTRA_FULLNAME);
mAccessToken = getIntent().getStringExtra(EXTRA_ACCESS_TOKEN);
titleTextView.setText(getIntent().getStringExtra(EXTRA_TITLE));
mPostContent = getIntent().getStringExtra(EXTRA_CONTENT);
contentEditText.setText(mPostContent);
contentEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm.isActive() && imm.isAcceptingText()) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
}
/**
* 如果输入法已经显示,那么就隐藏它;如果输入法现在没显示,那么就显示它
*/
public static void toggle(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 开关输入法
*
* @param currentFocusView 当前焦点view
*/
public static void toggleSoftInput(View currentFocusView) {
InputMethodManager imm = (InputMethodManager) currentFocusView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(currentFocusView, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
public static void toggleSoftInput(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
public static void toggleSoftInput(Context context) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public static void toggleSoftInput(Context context) {
InputMethodManager inputmanger = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputmanger != null) {
inputmanger.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS );
}
}
/**
* 控制键盘显隐,如果输入法在窗口上已经显示,则隐藏,反之则显示
*
* @param context
*/
public static void toggleInputKeyboard(Context context) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}