Android 手动显示和隐藏软键盘 点击空白处隐藏键盘

本文介绍在Android应用中如何控制软键盘的显示与隐藏,包括多种方法实现点击空白区域隐藏键盘的功能,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);


InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

2、方法二(view为接受软键盘输入的视图,SHOW_FORCED表示强制显示)

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);

imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //强制隐藏键盘



3、调用隐藏系统默认的输入法


((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
	.hideSoftInputFromWindow(
		WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), 
		InputMethodManager.HIDE_NOT_ALWAYS);
  (WidgetSearchActivity是当前的Activity)


((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  (WidgetSearchActivity是当前的Activity)


4、获取输入法打开的状态

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开


二、======================================================================
点击空白处隐藏键盘

1.实现方法一:

    android:focusable="true"
	android:focusableInTouchMode="true"
	android:clickable="true"

eg:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clipToPadding="true"
    android:fitsSystemWindows="true"
    android:focusable="true"
	android:focusableInTouchMode="true"
	android:clickable="true"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <RelativeLayout style="@style/rcs_actionbar" >


2.实现方法二:
通过给当前界面布局文件的父layout设置点击事件(相当于给整个Activity设置点击事件),
在事件里进行键盘隐藏

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/traceroute_rootview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:clickable="true"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

</LinearLayout>

java代码:
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.traceroute_rootview:
			 InputMethodManager imm = (InputMethodManager)
			 getSystemService(Context.INPUT_METHOD_SERVICE);
			 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
			break;
		}

	}


3.实现方法三:
通过dispatchTouchEvent每次ACTION_DOWN事件中动态判断非EditText本身区域的点击事件,然后在事件中进行屏蔽。

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			View v = getCurrentFocus();
			if (isShouldHideInput(v, ev)) {

				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				if (imm != null) {
					imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
				}
			}
			return super.dispatchTouchEvent(ev);
		}
		// 必不可少,否则所有的组件都不会有TouchEvent了
		if (getWindow().superDispatchTouchEvent(ev)) {
			return true;
		}
		return onTouchEvent(ev);
	}


isShoudHideInput(View v,MotionEvent e)方法:

	public  boolean isShouldHideInput(View v, MotionEvent event) {
		if (v != null && (v instanceof EditText)) {
			int[] leftTop = { 0, 0 };
			//获取输入框当前的location位置
			v.getLocationInWindow(leftTop);
			int left = leftTop[0];
			int top = leftTop[1];
			int bottom = top + v.getHeight();
			int right = left + v.getWidth();
			if (event.getX() > left && event.getX() < right
					&& event.getY() > top && event.getY() < bottom) {
				// 点击的是输入框区域,保留点击EditText的事件
				return false;
			} else {
				return true;
			}
		}
		return false;
	}








评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值