android - 在 Android 上设置包装组件的焦点顺序
问题描述
使用自定义组件时,我无法控制焦点顺序。具体来说,当我用不可聚焦的组件包装 EditText,然后将两个放在水平 LinearLayout 中时,只有第一个会获得焦点。(我在上面添加了其他功能;下面的代码是一个 MRE。)
我试过使用 nextFocusForward 和 nextFocusDown 来设置焦点顺序,我已经确认它在原始 EditTexts 上可以正常工作,但在自定义视图上却没有。我也尝试将属性传递给 EditText,但这也不起作用。我也尝试过让我的自定义视图具有焦点,但它没有接收焦点事件(我认为是因为 EditText 接收它们)并且有同样的问题。
编辑text_wrapper.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
tools:text="test"
/>
</LinearLayout>
EditTextWrapper.java:
public class EditTextWrapper extends LinearLayout {
public EditTextWrapper(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.edittext_wrapper, this, true);
}
}
活动测试.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.....EditTextWrapper
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.....EditTextWrapper
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:nextFocusDown="@id/right"
/>
<com.....EditTextWrapper
android:id="@+id/right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
</LinearLayout>
<com.....EditTextWrapper
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
我期待当光标位于第二个 EditText 并且您按下键盘上的下一个键时,它会转到第三个,但它会转到第四个。
我怀疑手动设置 nextFocusDown 的 id 不起作用的原因是因为我使用的是不可聚焦的包装类的 ID。我很不确定接下来应该尝试什么。
解决方案
我已检查更改您的 edittextwrapper.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/app_name"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
tools:text="test" />
您也可以将其用于焦点小部件
view.requestFocus();
推荐阅读
- excel - 使用唯一标识符基于行和列计数的单元格
- php - Docker 中的 Nginx + PHP-FPM + MySql 和子文件夹中的 Opencart 的“404 not found”
- generics - 要求一个可能为空的泛型不为空
- spring-boot - 如何在 application.yaml 键中转义等号
- c - 当您将数组元素移出数组范围时会发生什么?
- forms - 使用 GET 参数制作过滤器
- listview - 如何使用 TapGestureRecognizer 在列表视图中获取选定项
- arrays - 在 NSMutableArray 中获取最小双精度值的索引
- android - 进度条的大小不会改变
- acumatica - Acumatica REST API GET 突然抛出 SqlClient 语法错误