我在Android中有一个Activity
,其中包含两个元素:
-
EditText
-
ListView
当我的Activity
开始时, EditText
立即具有输入焦点(光标闪烁)。 我不希望任何控件在启动时就将焦点放在输入上。 我试过了:
EditText.setSelected(false);
EditText.setFocusable(false);
没运气。 当Activity
开始时,如何说服EditText
不要选择自身?
#1楼
下面的内容将使编辑文本在创建时无法聚焦,但在触摸它们时将其抓住。
<EditText
android:id="@+id/et_bonus_custom"
android:focusable="false" />
因此,您在xml中将focusable设置为false,但关键是在Java中,您添加了以下侦听器:
etBonus.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
return false;
}
});
因为您返回的是false,即不消耗事件,所以聚焦行为将像往常一样进行。
#2楼
我需要以编程方式清除所有领域的重点。 我只是将以下两个语句添加到我的主布局定义中。
myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
myLayout.setFocusableInTouchMode(true);
而已。 立即解决了我的问题。 谢谢,西尔弗(Silver)为我指明了正确的方向。
#3楼
我已经尝试了几个答案,但是重点仍然放在EditText上。 我只能通过同时使用以下两个解决方案来解决该问题。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
(引用自Silver https://stackoverflow.com/a/8639921/15695 )
并删除
<requestFocus />
在EditText
(参考来自floydaddict https://stackoverflow.com/a/9681809 )
#4楼
我做的最简单的事情是将焦点放在onCreate的另一个视图上:
myView.setFocusableInTouchMode(true);
myView.requestFocus();
这阻止了软键盘的出现,并且EditText中没有光标闪烁。
#5楼
尝试使用clearFocus()而不是setSelected(false)
。 Android中的每个视图都具有可聚焦性和可选择性,我认为您只是想清除焦点。
#6楼
我发现的唯一解决方案是:
- 创建一个LinearLayout (我不知道其他类型的Layout是否可以工作)
- 设置属性
android:focusable="true"
和android:focusableInTouchMode="true"
开始活动后, EditText
将无法获得焦点
#7楼
是的,我做了同样的事情-创建一个“虚拟”线性布局,该布局得到了最初的关注。 此外,我设置了“下一个”焦点ID,因此用户在滚动一次后便无法再对其进行焦点:
<LinearLayout 'dummy'>
<EditText et>
dummy.setNextFocusDownId(et.getId());
dummy.setNextFocusUpId(et.getId());
et.setNextFocusUpId(et.getId());
为了摆脱对视图的关注而进行的大量工作。
谢谢
#8楼
由于我不想使用与功能相关的东西来污染XML,因此我创建了这种方法,该方法“透明地”从第一个可聚焦的视图中窃取了焦点,然后确保在必要时将其自身删除!
public static View preventInitialFocus(final Activity activity)
{
final ViewGroup content = (ViewGroup)activity.findViewById(android.R.id.content);
final View root = content.getChildAt(0);
if (root == null) return null;
final View focusDummy = new View(activity);
final View.OnFocusChangeListener onFocusChangeListener = new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View view, boolean b)
{
view.setOnFocusChangeListener(null);
content.removeView(focusDummy);
}
};
focusDummy.setFocusable(true);
focusDummy.setFocusableInTouchMode(true);
content.addView(focusDummy, 0, new LinearLayout.LayoutParams(0, 0));
if (root instanceof ViewGroup)
{
final ViewGroup _root = (ViewGroup)root;
for (int i = 1, children = _root.getChildCount(); i < children; i++)
{
final View child = _root.getChildAt(i);
if (child.isFocusable() || child.isFocusableInTouchMode())
{
child.setOnFocusChangeListener(onFocusChangeListener);
break;
}
}
}
else if (root.isFocusable() || root.isFocusableInTouchMode())
root.setOnFocusChangeListener(onFocusChangeListener);
return focusDummy;
}
#9楼
来自Luc和Mark的出色答案,但是缺少一个好的代码示例。 像以下示例一样,将标签android:focusableInTouchMode="true"
到<LinearLayout>
可以解决此问题。
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
to prevent the dummy from receiving focus again -->
<AutoCompleteTextView android:id="@+id/autotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:nextFocusUp="@id/autotext"
android:nextFocusLeft="@id/autotext"/>
#10楼
在Activity的onCreate
上,只需在EditText元素上添加use clearFocus()
。 例如,
edittext = (EditText) findViewById(R.id.edittext);
edittext.clearFocus();
如果要将焦点转移到另一个元素,请在该元素上使用requestFocus()
。 例如,
button = (Button) findViewById(R.id.button);
button.requestFocus();
#11楼
简单的解决方案:在AndroidManifest
中使用Activity
标签
android:windowSoftInputMode="stateAlwaysHidden"
#12楼
以下在Manifest
为我工作。 写,
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateAlwaysHidden"/>
#13楼
对我来说,在所有设备上起作用的是:
<!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
<View
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true" />
只需将其作为有问题的聚焦视图之前的视图即可,仅此而已。
#14楼
您是否真的不希望集中注意力于实际问题? 或者您不希望由于聚焦EditText
而使其显示虚拟键盘? 我确实没有看到EditText
专注于开始的问题,但是当用户没有明确要求专注于EditText
(并因此打开键盘)时,打开softInput窗口绝对是一个问题。
如果是虚拟键盘的问题,请参阅AndroidManifest.xml
<activity>元素文档。
android:windowSoftInputMode="stateHidden"
-进入活动时始终将其隐藏。
或android:windowSoftInputMode="stateUnchanged"
-请勿更改(例如,如果尚未显示,则不显示它,但如果在进入活动时已打开,则将其保持打开状态)。
#15楼
这是最完美,最简单的解决方案。我一直在我的应用程序中使用它。
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
#16楼
您可以通过创建一个虚拟的EditText
来实现此目的,将其宽度和高度设置为0dp
,并将焦点请求到该视图。 在您的xml布局中添加以下代码段:
<EditText
android:id="@+id/editText0"
android:layout_width="0dp"
android:layout_height="0dp"
android:hint="@string/dummy"
android:ems="10"
>
<requestFocus />
</EditText>
#17楼
在Manifest.xml
文件的活动标记中添加android:windowSoftInputMode="stateAlwaysHidden"
。
#18楼
在onCreate
方法中添加以下内容:
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
#19楼
将此代码写在不想打开键盘的“ Activity
Manifest
中的Manifest
文件中。
android:windowSoftInputMode="stateHidden"
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.projectt"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
**android:windowSoftInputMode="stateHidden"**
android:label="@string/app_name" >
</activity>
</application>
</manifest>
#20楼
将此行写在您的家长版式中...
android:focusableInTouchMode="true"
#21楼
迟了,但可能有帮助。 在布局的顶部创建一个虚拟EditText,然后在onCreate()
调用myDummyEditText.requestFocus()
onCreate()
<EditText android:id="@+id/dummyEditTextFocus"
android:layout_width="0px"
android:layout_height="0px" />
这似乎符合我的预期。 无需处理配置更改等。对于具有冗长TextView(指令)的Activity,我需要此配置。
#22楼
最新但最简单的答案是,只需将其添加到XML的父级布局中即可。
android:focusable="true"
android:focusableInTouchMode="true"
如果有帮助,请支持! 快乐编码:)
#23楼
当按下按钮时,我使用以下代码阻止EditText窃取焦点。
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
View focused = internalWrapper.getFocusedChild();
focused.setVisibility(GONE);
v.requestFocus();
addPanel();
focused.setVisibility(VISIBLE);
}
});
基本上,隐藏编辑文本,然后再次显示它。 这对我有用,因为EditText 不在视图中,因此无论是否显示都没有关系。
您可以尝试隐藏并连续显示它,以查看是否有助于失去焦点。
#24楼
如果您对活动有另一个视图(如ListView
,则还可以执行以下操作:
ListView.requestFocus();
在您的onResume()
以从editText
获取焦点。
我知道这个问题已经回答,但只是提供了一个对我有用的替代解决方案:)
#25楼
根据其他海报提供的信息,我使用以下解决方案:
在布局XML中
<!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
<LinearLayout
android:id="@+id/linearLayout_focus"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="0px"
android:layout_height="0px"/>
<!-- AUTOCOMPLETE -->
<AutoCompleteTextView
android:id="@+id/autocomplete"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:inputType="textNoSuggestions|textVisiblePassword"/>
在onCreate()中
private AutoCompleteTextView mAutoCompleteTextView;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
//get references to UI components
mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
}
最后,在onResume()中
@Override
protected void onResume() {
super.onResume();
//do not give the editbox focus automatically when activity starts
mAutoCompleteTextView.clearFocus();
mLinearLayout.requestFocus();
}
#26楼
在您的第一个可编辑字段之前尝试以下操作:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
----
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
#27楼
您只需在layout
的第一个TextView
上将“ focusable”和“在触摸模式下 可聚焦”设置为true即可。 这样,当活动开始TextView
将集中,但由于其性质,什么也看不到集中在屏幕上,当然,也就没有键盘显示...
#28楼
这些解决方案都不适合我。 我解决自动对焦的方法是:
<activity android:name=".android.InviteFriendsActivity" android:windowSoftInputMode="adjustPan">
<intent-filter >
</intent-filter>
</activity>
#29楼
存在一个更简单的解决方案。 在父级布局中设置以下属性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true" >
现在,当活动开始时,默认情况下,此主布局将获得焦点。
另外,我们可以在运行时(例如,在完成子级编辑之后)从子级视图中删除焦点,方法是再次将焦点赋予主布局,如下所示:
findViewById(R.id.mainLayout).requestFocus();
Guillaume Perrot的 好评 :
android:descendantFocusability="beforeDescendants"
似乎是默认设置(整数值为0)。 只需添加android:focusableInTouchMode="true"
。
确实,我们可以看到在ViewGroup.initViewGroup()
方法(Android 2.2.2)中将beforeDescendants
设置为默认值。 但不等于ViewGroup.FOCUS_BEFORE_DESCENDANTS = 0x20000;
多亏纪尧姆。
#30楼
<TextView
android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
style="@android:style/Widget.EditText"/>
#31楼
问题似乎来自只能在布局的XML form
中看到的属性。
确保在EditText
XML标记内的声明末尾删除此行:
<requestFocus />
那应该给这样的东西:
<EditText
android:id="@+id/emailField"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress">
//<requestFocus /> /* <-- without this line */
</EditText>