android-如何从单个editText移除焦点
在我的应用程序中,我只有一个button.seFocusableInTouchMode(),以及一些button.requestFocus(),按钮和一个微调器。 我认为,我的EditText获得关注,因为它是此活动中唯一可关注的视图。 我的EditText在字段上显示带有橙色边框和光标。
现在,我想从此字段中删除焦点(我不希望显示光标和边框)。 有没有办法做到这一点?
通过执行button.seFocusableInTouchMode()和button.requestFocus(),我能够专注于按钮。但这突出了按钮,显然不是我想要的。
tobbenb3 asked 2020-01-01T03:23:17Z
16个解决方案
44 votes
您是否尝试过使用旧商品View.clearFocus()
MishaZ answered 2020-01-01T03:23:46Z
38 votes
android的新手。
getWindow().getDecorView().clearFocus();
这个对我有用..
只需添加..您的布局应具有:
android:focusable="true"
android:focusableInTouchMode="true"
Zaraki answered 2020-01-01T03:24:15Z
22 votes
检查此问题和选定的答案:阻止EditText专注于Activity启动。这很丑陋,但是有效,据我所知,没有更好的解决方案。
Maragues answered 2020-01-01T03:23:26Z
15 votes
我将尝试通过更多详细信息和理解来说明如何从EditText视图中删除焦点(闪烁的光标)。 通常这行代码应该可以工作
editText.clearFocus()
但可能是editText仍具有焦点的情况,这是因为clearFocus()方法试图将焦点设置回活动/片段布局中的第一个可聚焦视图。
因此,如果您在活动中只有一个可聚焦的视图,并且通常是您的EditText视图,则clearFocus()将焦点再次设置为该视图,并且对您来说,clearFocus()无法正常工作。请记住,默认情况下,EditText视图是focusable(true),因此,如果布局中只有一个EditText视图,它将无法在屏幕上获得焦点。 在这种情况下,您的解决方案将是在布局文件中找到父视图(某些布局,例如LinearLayout,Framelayout),然后将此xml代码设置为该父视图
android:focusable="true"
android:focusableInTouchMode="true"
之后,当您执行editText.clearFocus()时,布局内的父视图将接受焦点,并且您的editText将清除焦点。
我希望这将有助于某人了解clearFocus()的工作方式。
Sniper answered 2020-01-01T03:24:54Z
3 votes
我知道为时已晚,但对于有同样需求的人,您正在寻找editText.setFocusable(false)。
lm2a answered 2020-01-01T03:25:15Z
2 votes
使用附带的代码将焦点移到“其他人”上,如果您只想关闭键盘并释放焦点,而不必在意谁会得到,则可以这样做。
像这样使用:FocusHelper.releaseFocus(viewToReleaseFocusFrom)
public class FocusHelper {
public static void releaseFocus(View view) {
ViewParent parent = view.getParent();
ViewGroup group = null;
View child = null;
while (parent != null) {
if (parent instanceof ViewGroup) {
group = (ViewGroup) parent;
for (int i = 0; i < group.getChildCount(); i++) {
child = group.getChildAt(i);
if(child != view && child.isFocusable())
child.requestFocus();
}
}
parent = parent.getParent();
}
}
}
文件:该方法从子视图到视图树遍历,并寻找第一个要聚焦的子对象。
编辑:您还可以为此使用API:
View focusableView = v.focusSearch(View.FOCUS_DOWN);
if(focusableView != null) focusableView.requestFocus();
Sveinung Kval Bakken answered 2020-01-01T03:25:48Z
2 votes
我在editText上也遇到了类似的问题,自从活动开始以来,它就获得了关注。 我很容易解决此问题,如下所示:
您将这段代码添加到包含xml中editText的布局中:
android:id="@+id/linearlayout"
android:focusableInTouchMode="true"
不要忘记android:id,没有它我有一个错误。
我对editText的另一个问题是,一旦它获得了第一个焦点,焦点就永远不会消失。 这是我的Java代码的一部分,它有一个editText和一个捕获editText中的文本的按钮:
editText=(EditText) findViewById(R.id.et1);
tvhome= (TextView)findViewById(R.id.tv_home);
etBtn= (Button) findViewById(R.id.btn_homeadd);
etBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tvhome.setText( editText.getText().toString() );
//** this code is for hiding the keyboard after pressing the button
View view = Settings.this.getCurrentFocus();
if (view != null)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
//**
editText.getText().clear();//clears the text
editText.setFocusable(false);//disables the focus of the editText
Log.i("onCreate().Button.onClickListener()", "et.isfocused= "+editText.isFocused());
}
});
editText.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(v.getId() == R.id.et1)
{
v.setFocusableInTouchMode(true);// when the editText is clicked it will gain focus again
//** this code is for enabling the keyboard at the first click on the editText
if(v.isFocused())//the code is optional, because at the second click the keyboard shows by itself
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
//**
Log.i("onCreate().EditText.onClickListener()", "et.isfocused= "+v.isFocused());
}
else
Log.i("onCreate().EditText.onClickListener()", "the listener did'nt consume the event");
}
});
希望对您有些帮助!
Lena answered 2020-01-01T03:26:26Z
2 votes
只是找到另一个视图并给予焦点即可。
var refresher = FindViewById(Resource.Id.refresher);
refresher.RequestFocus();
PmanAce answered 2020-01-01T03:26:46Z
2 votes
如果Edittext父级布局是Linear,则添加
android:focusable="true"
android:focusableInTouchMode="true"
像下面
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true">
............
当Edittext父级布局为相对时,则
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
喜欢
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">
............
OmiK answered 2020-01-01T03:27:19Z
1 votes
我已经做了很多尝试来清除编辑文本的焦点。 clearfocus()和focusable等东西对我没有用。因此,我想到了让假的edittext获得关注的想法:
...
android:layout_width="match_parent"
android:layout_height="match_parent">
...
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/fake"
android:textSize="1sp"/>
然后在您的Java代码中:
View view = Activity.this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
fake.requestFocus();
}
它会隐藏键盘,并移走具有该键盘的所有edittext的焦点。 而且正如您所看到的,假的edittext在屏幕外,无法看到
navid answered 2020-01-01T03:27:49Z
0 votes
只要包括这行
android:selectAllOnFocus="false"
在与EditText布局相对应的XML段中。
harshvardhan answered 2020-01-01T03:28:14Z
0 votes
您只需要从视图中清除焦点即可
EditText.clearFocus()
Mayank Bhatnagar answered 2020-01-01T03:28:34Z
0 votes
如果我正确理解了您的问题,这应该可以帮助您:
TextView tv1 = (TextView) findViewById(R.id.tv1);
tv1 .setFocusable(false);
kPieczonka answered 2020-01-01T03:28:54Z
0 votes
由于我是在小部件中而不是在活动中,所以我做了:
`getRootView()。clearFocus();
kingston answered 2020-01-01T03:29:18Z
0 votes
android:clickable="false"
android:focusable="false"
android:textSize="40dp"
android:textAlignment="center"
android:textStyle="bold"
android:textAppearance="@style/Base.Theme.AppCompat.Light.DarkActionBar"
android:text="AVIATORS"/>
vithika answered 2020-01-01T03:29:34Z
0 votes
您只需要使用属性设置ViewGroup:
android:focusableInTouchMode="true"
ViewGroup是包含每个子视图的布局。
GaijinForce answered 2020-01-01T03:30:03Z