首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SetFocus不能正常工作

SetFocus不能正常工作
EN

Stack Overflow用户
提问于 2017-12-20 16:17:44
回答 1查看 61关注 0票数 0

我在我的应用上有一个扫描功能。我有一个要用条形码扫描仪(外接键盘)扫描的输入域。扫描成功后,焦点应返回到此输入字段。

在片段中:

代码语言:javascript
复制
 EditText handscann=rootView.findViewById(R.id.handscannerinput);
 handscann.requestFocus();

在xml中:

代码语言:javascript
复制
<EditText
    android:id="@+id/handscannerinput"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:imeOptions="actionSearch"
    android:inputType="numberDecimal"
    android:singleLine="true"
    android:text="@{vmScann.handscannerArticlenumber}"
    android:theme="@style/EditTextTheme">
    <requestFocus />
</EditText>

我的问题是,焦点不是设置在这个输入字段上,而是设置在同一个视图上的一个切换器上。

当我触摸开关时,焦点转到输入字段。该切换器具有另一个id:

代码语言:javascript
复制
 <Switch
    android:id="@+id/switch2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:checked="@={vmScann.myArticleSwitchActivated}"
    android:theme="@style/SwitchThemeGreen"
    android:paddingRight="15dp" />

有没有人知道为什么会这样?

编辑:我在请求焦点后检查视图是否具有焦点

代码语言:javascript
复制
View view=rootView.findFocus(); 

这说明重点放在手写扫描仪输入上。

之后,我使用hasfocus()进行检查,但这是false

代码语言:javascript
复制
if(handscann.hasFocus()){
    int i=0; //for the breakpoint
}
EN

回答 1

Stack Overflow用户

发布于 2017-12-20 16:27:31

初始化并转换onViewCreated()中的handscann或片段的onCreateView()函数,扫描成功后添加requestFocus()

在回调函数内扫描调用requestFocus()的ie onFinsih

代码语言:javascript
复制
your callBackFunction(){ 

    //Do Opertions
    yourSwitch.clearFocus();
    handscann.setText(text); //If any
    handscann.requestFocus();
}
@Override
public void onResume() {
    super.onResume();
    handscann.requestFocus();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47901545

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档