如何清除EditText的焦点? [英] How to clear focus for EditText?

查看:878
本文介绍了如何清除EditText的焦点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,其中的搜索框(EditText)位于顶部,而ListView位于下方.每当活动开始时,EditText始终具有焦点并调出部分覆盖ListView的键盘.

I have an activity with a search box (EditText) on the top, and a ListView below. Whenever the activity starts, the EditText always has focus and bring up the keyboard which partially cover the ListView.

没有其他可以聚焦的文本视图. 我希望EditText仅在用户触摸它并开始键入时才具有焦点.我尝试将clearFocus()放入onCreateViewonViewCreatedonCreated,但是没有运气.

There's no other text view that can have focus. I want the EditText to have focus ONLY when the user touches it and start typing. I try to put clearFocus() in onCreateView, onViewCreated or onCreated, but no luck.

推荐答案

在父布局中设置下一个属性:

Set in your parent layout next attributes:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true" >

现在,当activity开始此布局时,将获得默认焦点.

And now, when activity starts this layout getting default focus.

我们还可以在运行时(例如,在完成子级编辑后)从子级视图中移除焦点:

Also we can remove focus from children views in runtime (e.g. after finishing child editing):

findViewById(R.id.mainLayout).requestFocus();

查看AndroidManifest.xml元素.

Look in the AndroidManifest.xml element.

android:windowSoftInputMode="stateHidden"

进入活动时,它总是隐藏键盘.

It always hide key board when entering the activity.

这篇关于如何清除EditText的焦点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆