android代码移除焦点,android-如何从单个editText移除焦点
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
android代码移除焦点,android-如何从单个editText移除焦点相关推荐
- Android 系统性能优化(42)---Android代码内存优化建议-Android资源篇
Android代码内存优化建议-Android资源篇 这篇文章主要介绍在实际Android应用程序的开发中,容易导致内存泄露的一些情况.开发人员如果在进行代码编写之前就有内存泄露方面的基础知识,那么写 ...
- android聚焦时如何给控件加边框,edittext设置获得焦点时的边框颜色
第一步:为了更好的比较,准备两个一模一样的EditText(当Activity启动时,焦点会在第一个EditText上,如果你不希望这样只需要写一个高度和宽带为0的EditText即可避免,这里就不这 ...
- android 代码获取图片信息吗,Android 通过网络获取图片的代码
Android 通过网络获取图片的代码 主activity package com.netimg; import android.app.Activity; import android.graphi ...
- android 代码浏览,Webview实现android简单的浏览器实例代码
WebView是Android中一个非常实用的组件,它和Safai.Chrome一样都是基于Webkit网页渲染引擎,可以通过加载HTML数据的方式便捷地展现软件的界面,下面通过本文给大家介绍Webv ...
- android 代码设置像素,【Android实例】用设计原则来重构1像素保活代码
1 类图 在[FJU项目]1像素进程保活(二)中,涉及到的几个类的类图如下所示(仅供参考): 实线箭头:关联 虚线箭头:依赖 重构前UML类图在上图中,OnePixelManager里面有太多的职责, ...
- android代码zip怎么用,Android平台实现Zip文件解压缩
[android]代码库在Android平台中如何实现Zip文件的解压缩功能呢? 因为Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩还是比较简单的,下面给大家一个解压缩zi ...
- android代码修改excel文件格式,Android实现类似execel的表格 能回显并能修改表格内容的方法...
如下所示: 自定义实现一个水平滚动控件HorizontalScrollView package com.example.view; import android.content.Context; im ...
- android代码获取应用名称,Android获取应用程序名称(ApplicationName)
MainActivity如下: package cn.testapplicationname; import android.os.Bundle; import android.widget.Text ...
- [最新] Android 代码规范大全(Android开发速看),2021年最新大厂Android面试笔试题目
| 数据库类 | 功能名 + DBHelper | 新闻数据库:NewsDBHelper | | 自定义的共享基础类 | Base + 基础 | BaseActivity, BaseFragment ...
最新文章
- sleep() 函数
- request,logging,ConfigParser——接口框架
- python3多进程 pool manager_Python多进程multiprocessing.Pool
- Win8下怎样安装Win7 or Win7下怎样安装win8?
- Java流程控制02 选择结构 if结构 switch结构
- Linux 能否拿下苹果 M1 阵地?
- 训练集、验证集、测试集
- 前端复习-02-ajax原生以及jq和跨域方面的应用。
- java实现modbus rtu协议与 modscan等工具(3)物理连接
- 省级-上市公司数字经济数据(2013-2020年)
- excel 置信区间 计算_正态分布 excle(Excel中用什么函数可以算置信区间,怎么算啊?)...
- 卡西欧计算机怎么进制转换,卡西欧计算机怎么把十进制转换二进制
- 计算机无法用u盘重装系统,最简单不用U盘电脑重装系统教程
- Java基础之序列化
- discuz接入七牛sdk
- uhs3内存卡有哪些_高速内存卡是什么 3款热门高速内存卡推荐
- Python学习之旅-15
- java escpos_如何在Linux中将ESC / POS命令发送到热敏打印机
- 14. Juju and Binary String
- 探秘Linux特殊设备文件:(/dev/null,/dev/zero,/dev/random,/dev/urandom等)