我在我的应用上有一个扫描功能。我有一个要用条形码扫描仪(外接键盘)扫描的输入域。扫描成功后,焦点应返回到此输入字段。
在片段中:
EditText handscann=rootView.findViewById(R.id.handscannerinput);
handscann.requestFocus();在xml中:
<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:
<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" />有没有人知道为什么会这样?
编辑:我在请求焦点后检查视图是否具有焦点
View view=rootView.findFocus(); 这说明重点放在手写扫描仪输入上。
之后,我使用hasfocus()进行检查,但这是false
if(handscann.hasFocus()){
int i=0; //for the breakpoint
}发布于 2017-12-20 16:27:31
初始化并转换onViewCreated()中的handscann或片段的onCreateView()函数,扫描成功后添加requestFocus()
在回调函数内扫描调用requestFocus()的ie onFinsih
your callBackFunction(){
//Do Opertions
yourSwitch.clearFocus();
handscann.setText(text); //If any
handscann.requestFocus();
}
@Override
public void onResume() {
super.onResume();
handscann.requestFocus();
}https://stackoverflow.com/questions/47901545
复制相似问题