的父布局中添加
android:focusable="true"
和
android:focusableInTouchMode="true"
元素,如下;
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout7" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true" android:focusableInTouchMode="true">
我想,应该对你有帮助。
如果您想清除编辑文本上的默认焦点,请使用以下 2 个属性
android:focusable="false"
android:focusableInTouchMode="true"
在父线性布局内。
例如:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="true"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="email id"
android:inputType="textEmailAddress"
android:maxLines="1"
/>
</LinearLayout>
如果您想在创建活动时隐藏键盘,请在清单文件活动标记中使用这些属性 例如:
<activity
android:configChanges="screenSize|orientation|keyboardHidden"
android:screenOrientation="portrait"
android:name=".activities.LoginActivity"
android:windowSoftInputMode="stateHidden|adjustResize"/>
如果您想在单击按钮或发生某些事件时隐藏键盘,请使用以下代码
public void onClick(View v) {
try {
//InputMethodManager is used to hide the virtual keyboard from the user after finishing the user input
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isAcceptingText()) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
} catch (NullPointerException e) {
Log.e("Exception", e.getMessage() + ">>");
}
}
} catch (NullPointerException e) {
Log.e("Exception", e.getMessage() + ">>");
}
如果您想在离开活动后将焦点从编辑文本字段中移开
@Override
protected void onResume() {
super.onResume();
mEmail.clearFocus();
mPassword.clearFocus();
}
最后,如果您想在提交表单时清除编辑文本字段中的数据,请使用
@Override
protected void onResume() {
super.onResume();
mEmail.getText().clear();
mPassword.getText().clear();
}
无需更多工作...只需添加 android:windowSoftInputMode="stateAlwaysHidden" 到 Manifest.xml 文件的活动标签。
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px" />
<EditText android:text=""
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hint">
</EditText>
无需其他编码,这是一个简单明了的解决方案。
您可以使用隐藏窗口软输入模式来隐藏键盘和
android:focusable="false"
和android:focusableInTouchMode="true"
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在 LinearLayout 中尝试此代码
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:hint="Search..."
android:drawableRight="@drawable/ic_search"
android:id="@+id/search_tab_hotbird_F"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyHotbird_F"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
它对我有用,希望也能帮助你。
要么:
android:windowSoftInputMode="stateAlwaysHidden"
将此属性添加到要禁用自动对焦的特定活动下的 Manifest.xml 文件中。 缺点:它只会隐藏您的软键盘,光标仍然在那里。
其他:
android:focusableInTouchMode="true"
将此属性添加到您的 UI xml 文件(在父布局下),以便它将隐藏焦点以及键盘。
<EditText
android:id="@+id/input"
android:layout_width="0dp"
android:layout_height="48dp"
android:focusable="false"
android:focusableInTouchMode="false" />
detailInputView = (EditText) debugToolsView.findViewById(R.id.input);
detailInputView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
detailInputView.setFocusable(true);
detailInputView.setFocusableInTouchMode(true);
return false;
}
});
只需将以下内容添加到主布局视图 xml 标签中:
android:focusableInTouchMode="true"
嘿李,
有几种方法可以做到这一点,Mudassir已经为您提供了一种方法。
如果这个想法行不通,那就做简单的事-
在您的
AndroidManifest>Activity
中添加以下简单代码:
android:windowSoftInputMode="stateHidden"
XML 中的 EditText 无需执行任何操作,一切都会正常工作,并且不会出现焦点。
查看现实生活中的示例代码:
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
为了获得清晰的默认 requestFocuse,您应该像下面这样设置 View 的父 focusable="true"
<android.support.constraint.ConstraintLayout
android:id="@+id/coordinatorChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
在使父视图可见之前,我会执行此操作(并且工作正常),只要我触摸编辑文本,它就不会自动聚焦。 (parentView包含编辑文本)
...
parentView.setFocusable(true);
parentView.setFocusableInTouchMode(true);
objProperties.setVisibility(VISIBLE);
...
我知道我迟到了大约 133 个月,但对于那些想知道的人来说,这对我有用:
创建一个很小的随机对象(我选择了一个文本视图),并将 XML 属性
android:focusable="true"
和 android:focusableInTouchMode="true"
添加到您的代码中:
<TextView
android:id="@+id/focus_diversion"
android:layout_width="1px"
android:layout_height="1px"
android:focusable="true"
android:focusableInTouchMode="true" />
创建一个具有与上面相同属性的对象:
<TextView
android:id="@+id/focus_diversion"
android:layout_width="1px"
android:layout_height="1px"
android:focusable="true"
android:focusableInTouchMode="true" />
并且在 Java 中,每当您想要取消其他视图的焦点时添加请求焦点:
TextView decoy_focus = findViewById(R.id.focus_diversion);
decoy_focus.requestFocus();