Android: 力量 EditText 去除焦点?

我希望能够删除重点 EditText. 例如,如果出现键盘,并且用户使用后退按钮隐藏它,我想对焦并将光标消失。 我怎样才能做到这一点?
已邀请:

二哥

赞同来自:

您可以使光标和焦点消失


edittext.clearFocus//;


但是何时隐藏关键板时, - 这是艰苦的工作。

君笑尘

赞同来自:

你可以添加它
onCreate

, 它每次都会隐藏键盘 activty 开始。

您还可以以编程方式将焦点更改为另一个项目。


this.getWindow//.setSoftInputMode/WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN/;

君笑尘

赞同来自:

添加
LinearLayout

正面
EditText

在你的 XML.


<linearlayout android:clickable="true" android:focusable="true" android:focusableintouchmode="true" android:layout_height="0px" android:layout_width="0px"></linearlayout>


或者,您可以通过添加这些字符串来查看之前进行操作。 'EditText'.


<button android:focusable="true" android:focusableintouchmode="true" android:gravity="center" android:id="@+id/btnSearch" android:layout_height="50dp" android:layout_width="50dp" android:text="Quick Search" android:textcolor="#fff" android:textsize="13sp" android:textstyle="bold"></button>
<edittext android:gravity="left" android:hint="Name" android:id="@+id/edtSearch" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_marginright="5dp" android:layout_width="wrap_content" android:maxlines="1" android:singleline="true" android:textcolorhint="@color/blue" android:textsize="13sp" android:textstyle="bold"></edittext>

三叔

赞同来自:

删除焦点,但保持关注:


editText.setFocusableInTouchMode/false/;
editText.setFocusable/false/;
editText.setFocusableInTouchMode/true/;
editText.setFocusable/true/;


EditText 丢失焦点,但可以在新的触摸活动中再次得到它。

知食

赞同来自:

http://www.helloandroid.com/tu ... droid
这个对我有用

编辑

在他们提供的链接中 LinearLayout, 但一个简单的观点将有效


<view android:focusable="true" android:focusableintouchmode="true" android:id="@+id/focus_thief" android:layout_height="1dp" android:layout_width="1dp"></view>


然后,如果这 "thief" 放在布局的顶部 /成为第一元素/, 呼叫
clearFocus//

他们会工作。

知食

赞同来自:

将这两个属性添加到父布局 /例如:线性布局,相对布局/


android:focusable="true"
android:focusableInTouchMode="true"


它会让她的工作 :/

喜特乐

赞同来自:

你也可以打开

android:windowSoftInputMode="stateAlwaysHidden"

在清单的行动部分。

这是相同的 :


this.getWindow//.setSoftInputMode/WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN/;


但在XML的含义中。

FYI, 您还可以隐藏带有代码的键盘:


// hide virtual keyboard
InputMethodManager imm = /InputMethodManager/getSystemService/Context.INPUT_METHOD_SERVICE/;
imm.hideSoftInputFromWindow/mYourEditText.getWindowToken//, 0/;

帅驴

赞同来自:

尝试在演示文稿上使用它。
它为我工作:


<view android:focusable="true" android:focusableintouchmode="true" android:id="@+id/fucused" android:layout_height="wrap_content" android:layout_width="wrap_content"></view>

郭文康

赞同来自:

添加到您放置的父级布局
EditText

this
android:focusableInTouchMode="true"

詹大官人

赞同来自:

你必须删除
<requestfocus></requestfocus>


如果您不使用它和同样的问题

用户
LinearLayout

作为父母和集


android:focusable="true"
android:focusableInTouchMode="true"


我希望这能帮到您。

龙天

赞同来自:

在启动活动时隐藏键盘..写下以下代码 onCreate//..


InputMethodManager imm = /InputMethodManager/
getSystemService/Activity.INPUT_METHOD_SERVICE/;
imm.hideSoftInputFromWindow/getWindow//.getDecorView//.getWindowToken//, 0/;


清除焦点并从中删除光标 edittext.....


editText.clearFocus//;

editText.setCursorVisible/false/;

董宝中

赞同来自:

这是我第一个回答 SO, 如果有错误,那么对我来说不会太苛刻。 :D.

有几个答案漂浮在一起 SO, 但我觉得发表完整决定的愿望,因为它给我带来了疯狂。 我抓住了各方的碎片和碎片,所以如果我没有给每个人提供适当的贷款...... :/

/我简化了我的结果,因为在我的演示文稿中有太多物品,我不想用它垃圾邮件,我会尽量使它尽可能普遍....../

对于您的布局,您需要您的父母

EditText



父母视图

它定义如下:


<linearlayout android:descendantfocusability="beforeDescendants" android:focusableintouchmode="true" android:id="@+id/lytContainer" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="[url=http://schemas.android.com/apk/res/android">]http://schemas.android.com/apk ... gt%3B[/url]
<edittext android:clickable="true" android:gravity="center" android:hint="@string/enter_your_text" android:id="@+id/etEditor" android:inputtype="number" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="fill_parent" android:textalignment="center" android:textcolor="@android:color/darker_gray" android:textsize="12dp"></edittext>
</linearlayout>


所以,我需要在这里做一些事情。 我需要填写我的 EditText - 即 -

android:hint="hint"

还,


android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"


制作成这么做

EditText

没有专注于入口

行为

, 后来的

行动

配置后,此参数有助于安装

onTouchListener

在它窃取焦点

EditText

.

现在,在活动中:


package com.at.keyboardhide;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnTouchListener{
private EditText getEditText;
private LinearLayout getLinearLayout;
@Override
public void onCreate/Bundle savedInstanceState/ {
super.onCreate/savedInstanceState/;
this.getWindow//.setSoftInputMode/
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN/;

setContentView/R.layout.keyboardmain/;
getEditText = /EditText/findViewById/R.id.etEditor/;
getLinearLayout = /LinearLayout/findViewById/R.id.lytContainer/;
getLinearLayout.setOnTouchListener/this/;

getEditText.setOnEditorActionListener/new TextView.OnEditorActionListener// {
@Override
public boolean onEditorAction/TextView v, int actionId, KeyEvent event/ {
if /actionId == EditorInfo.IME_ACTION_DONE/ {
Log.d/"EDTA", "text was entered."/;
getEditText.clearFocus//;
imm.hideSoftInputFromWindow/barcodeNo.getWindowToken//, 0/;
return true;
}
return false;
}
}/;
}
@Override
public boolean onTouch/View v, MotionEvent event/ {
if/v==getLinearLayout/{
InputMethodManager imm = /InputMethodManager/getSystemService/Context.INPUT_METHOD_SERVICE/;
imm.hideSoftInputFromWindow/getEditText.getWindowToken//, 0/;
getEditText.clearFocus//;
return true;
}
return false;
}
}


我在这个页面上找到了几个答案,我发现了一个伴随着活动的活动
http://amitthaperandroidquery. ... 56529
. rest, 我错过了哪个,我不得不发现自己,被清除了专注

EditText

, 我在内部添加了哪个

setOnEditorActionListener

和 onTouchLister 为了

父母视图

.

我希望这将有助于某人并拯救他们的时间。 :/

卵子,
一个女婿。

涵秋

赞同来自:

在评论中,您询问是否有可能重点缩影另一个想法 EditText. 对的,这是可能的。 使用方法
.requestFocus//

对于您想要专注于开始的演示文稿 /在方法中 onCreate///

此外,另一个表示的重点将允许削减一定量的代码。 /例如,隐藏键盘的代码/

知食

赞同来自:

我有同样的问题。 它让我更疯狂。

我有一个先进的对话 ScrollView, 那有 TableLayout 延长 LinearLayout, 其中包含 SeekBar 和 EditText.

第一的 EditText 在显示对话框后以及编辑键盘上方的文本后,始终具有自动对焦 EditText 仍有焦点,键盘仍然可见。

我几乎尝试了这个话题的所有解决方案,他们都没有为我工作。

那是我的

一个简单的解决方案

: /text = EditText/


text.setOnEditorActionListener/ new OnEditorActionListener/ /{
public boolean onEditorAction/ TextView v, int actionId, KeyEvent event /{
if/ /event != null && event.getKeyCode// == KeyEvent.KEYCODE_ENTER/ ||
/actionId == EditorInfo.IME_ACTION_DONE/ /{
text.clearFocus/ /;
InputMethodManager iMgr = null;
iMgr = /InputMethodManager/mContext.getSystemService/ Context.INPUT_METHOD_SERVICE /;
iMgr.hideSoftInputFromWindow/ text.getWindowToken//, 0 /;
}
return true;
}
}/;


顺便问一下,我

不是

使用以下碎片中的任何一部分要解决:


//setFocusableInTouchMode/ true /
//setFocusable/ true /
//setDescendantFocusability/ ViewGroup.FOCUS_BEFORE_DESCENDANTS /


和我

不是

使用分离元件,例如宽度和高度的视图 1dp.

我希望这将有助于某人 :D

董宝中

赞同来自:

你也可以打开
android:windowSoftInputMode="stateAlwaysHidden"

在清单行动的一部分中。

这相当于:


this.getWindow//.setSoftInputMode/WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN/;

涵秋

赞同来自:

EditText.setOnKeyListener/new OnKeyListener// {
public boolean onKey/View view, int keyCode, KeyEvent event/ {
if /keyCode == KeyEvent.KEYCODE_ENTER/ {
InputMethodManager imm = /InputMethodManager/getSystemService/Context.INPUT_METHOD_SERVICE/; imm.hideSoftInputFromWindow/URLText.getWindowToken//, 0/;
EditText.setFocusable/false/;
EditText.setFocusableInTouchMode/true/;
return true;
} else {
return false;
}
}
}/;

冰洋

赞同来自:

editText.setFocusableInTouchMode/true/



EditText

当用户来到它时,将能够获得焦点。
当主要布局时 /活动,对话等/ 它变得可见
EditText

即使它是布局中的第一个视图,它也不会自动焦点。

二哥

赞同来自:

您可以通过安装该属性避免任何关注您的项目

android:descendantFocusability

父元素。

这是一个例子:


<scrollview android:descendantfocusability="blocksDescendants" android:id="@+id/search__scroller" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url] xmlns:tools="[url=http://schemas.android.com/tools">]http://schemas.android.com/tools">[/url]
</scrollview>


在这里归因于 android:descendantFocusability, 建立 "blocksDescendants", 块专注于儿童元素。

有关更多信息,您可以找到
https://developer.android.com/ ... %2529
.

董宝中

赞同来自:

check your xml file
<edittext android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textsize="14sp">

**<requestfocus></requestfocus>**
</edittext>


//Remove **<requestfocus></requestfocus>** from xml

要回复问题请先登录注册