Android软键盘处理方式工具类(隐藏、显示)

 问题:Fragment跳转activity(含editText),并给了焦点弹出了键盘,然后按返回关闭当前界面,软键盘未消失。

清单文件配置未设置

windowSoftInputMode
我用的第二个hideIme(),有效。你们自己建个工具类就能用了。
/**
	 * hide ime durationMillis
	 */
	public static long durationMillis = 250;

	/**
	 * show ime delayMillis
	 */
	public static long delayMillis = 250;

	/**
	 * hide ime
	 * @param activity
	 */
	public static void hideIme(Activity activity) {
		if (activity == null) {
			return;
		}

		hideIme(activity, durationMillis);
	}

	/**
	 * hide ime
	 * @param activity
	 * @param durationMillis
	 */
	public static void hideIme(Activity activity, long durationMillis) {
		if (activity == null) {
			return;
		}

		if (durationMillis < 0) {
			durationMillis = 0;
		}

		View view = activity.getWindow().peekDecorView();
		if (view != null && view.getWindowToken() != null) {
			InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
			TranslateAnimation ta = new TranslateAnimation(0, 0, 120, 0);
			ta.setDuration(durationMillis);
			ta.setInterpolator(new LinearInterpolator());
			ta.setFillBefore(true);
			view.setAnimation(ta);
			imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

		}
	}

	/**
	 * hide ime
	 * @param activity
	 */
	public static void hideImeThen(Activity activity) {
		if (activity == null) {
			return;
		}

		hideIme(activity, 0);
	}

	/**
	 * hide ime
	 * @param v
	 */
	public static void hideIme(View v) {
		if (v == null) return;

		Context context = v.getContext();
		if (context != null && v.getWindowToken() != null) {
			InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
		}
	}

	/**
	 * show ime
	 * @param context
	 */
	public static void showIme(Activity context) {
		if (context == null) return;

		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
		// imm.showSoftInput(context.getCurrentFocus(),
		// InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/**
	 * show ime
	 * @param v
	 */
	public static void showImeThen(View v) {
		showIme(v, 0);
	}

	/**
	 * show ime
	 * @param v
	 */
	public static void showIme(View v) {
		showIme(v, delayMillis);
	}

	/**
	 * show ime
	 * @param v
	 * @param delayMillis
	 */
	public static void showIme(final View v, long delayMillis) {
		if (v == null) return;

		final long dMillis = delayMillis <= 0 ? 0 : delayMillis;
		final Context context = v.getContext();
		if (context != null && v.isFocused()) {
			v.postDelayed(new Runnable() {

				@Override
				public void run() {
					InputMethodManager imm = (InputMethodManager) context
							.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.showSoftInput(v, InputMethodManager.HIDE_NOT_ALWAYS);
				}
			}, dMillis);
		}
	}

	/**
	 * ime is show
	 * 
	 * <pre>
	 * note:可能不准确
	 * </pre>
	 * @param context
	 * @return
	 */
	public static boolean isImeShow(Context context) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

		return imm.isActive();
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值