Android 在编辑文本时禁用自动聚焦
我有一个编辑文本:Android 在编辑文本时禁用自动聚焦,android,android-layout,android-edittext,autofocus,Android,Android Layout,Android Edittext,Autofocus,我有一个编辑文本: <LinearLayout android:id="@+id/linearLayout7" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_we
<LinearLayout android:id="@+id/linearLayout7" android:layout_width="match_parent" android:layout_height="wrap_content">
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1" android:id="@+id/editText1" android:text="3">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/button2"></Button>
</LinearLayout>
然后是下面的其他东西
我遇到的问题是,当应用程序启动时,焦点就在输入上,我不希望应用程序启动时焦点就在输入上
我试着取下那根绳子
<requestFocus></requestFocus>
在的父布局中添加和元素,如下所示
<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">
我想,这应该对你有帮助
另见没有更多的工作了。。。只需添加android:WindowsOfInputMode=“stateAllwayshidden” 指向Manifest.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;
}
});
detailInputView=(EditText)调试工具View.findViewById(R.id.input);
detailInputView.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
detailInputView.setFocusable(true);
detailInputView.setFocusableInTouchMode(true);
返回false;
}
});
只需将以下内容添加到主布局视图xml标记中:
android:focusableInTouchMode="true"
Hei,
有几种方法可以做到这一点,有一种方法可以让你做到这一点
如果这个想法行不通,那就做简单的事情-
在您的AndroidManifest>活动中
添加以下简单代码:
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>
android:windowSoftInputMode="stateAlwaysHidden"
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"/>
如果要在离开活动后从编辑文本字段中取消焦点
@Override
protected void onResume() {
super.onResume();
mEmail.clearFocus();
mPassword.clearFocus();
}
最后,如果要在提交表单时清除编辑文本字段中的数据,请使用
@Override
protected void onResume() {
super.onResume();
mEmail.getText().clear();
mPassword.getText().clear();
}
您可以使用隐藏的窗口软输入模式隐藏键盘和android:focusable=“false”和
android:focusableInTouchMode=“true”
在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>
它对我有用,希望也能帮助你 其中一个:
<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>
android:windowSoftInputMode="stateAlwaysHidden"
android:focusableInTouchMode="true"
将此属性添加到要禁用自动聚焦的特定活动下的Manifest.xml文件中。
缺点:它只会隐藏您的软键盘,光标仍会在那里
其他:
<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>
android:windowSoftInputMode="stateAlwaysHidden"
android:focusableInTouchMode="true"
将此属性添加到UI xml文件中(在父布局下),以便同时隐藏键盘的焦点 对于clear default requestFocuse,您应该如下设置视图的父视图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">
在显示父视图之前,我这样做(工作正常)编辑文本不会仅在触摸它时自动聚焦。(父视图包括编辑文本)
设置android:focusable=“false”。它不会把焦点放在你的编辑文本上,这会让你永远无法复制吗?现在,即使单击文本字段,它也不会聚焦。我想在点击时对焦,只是在应用程序启动时不会自动对焦。你可以先从获取按钮id开始,然后在代码中设置对焦按钮……我这样做了:a_按钮=(按钮)findViewById(R.id.a_按钮);a_按钮。requestFocus();代码刚开始的时候,它什么也没做。我可以通过xml设置焦点吗?在我的父布局上添加
android:focusableInTouchMode=“true”
对我来说很有效。@Saad:It有效。我在这里发布之前已经测试过了。将元素保持在XML文件的顶部。顺便说一句,您开发的API级别(或Android版本)是什么?很好。你能解释一下android:focusable和android:focusableInTouchMode的用途吗(我指的是区别和用法)。如果不起作用,也可以将其添加到父级:android:genderantFocusability=“beforemedints”
起作用,但当我点击编辑文本时,键盘不会弹出@你是对的,我也遇到了同样的问题。请解释这有什么作用,以及它如何帮助OP。没有解释的建议没有多大帮助,即使修复可以工作。参考此。。以上链接。这将隐藏键盘,但如果我没记错的话,EditText仍然保持焦点。API 27所有提到的方法都不起作用!但这也无助于从编辑文本字段获取焦点,它只是改变了设备上元素加载的方式,尽管它可以工作