2014/09/19

nextFocusForward は imeOptions に actionNext を設定していないと有効にならない

Android の EditText は属性に singleLine を指定していると、自動的に「次へ」というボタンがあらわれ1、入力後に押すと次の View へ移動してくれるようになります。

この機能、便利なのですが、時々自分の意図していない View に飛んでしまうことがあります。
たとえば、以下のような画面で、矢印のように遷移して欲しいとします。
ところが、デフォルトのままだと、右の View に移動してしまい、想定する動作にはなりません。

こういうときは、nextFocusForward という属性を使用すればよいのですが、単に指定しただけでは駄目です。
以下のように imeOptions に actionNext を指定しないと nextFocusForward は有効にならないのです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">
 
    <EditText
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello world!"
        android:singleLine="true"
        android:imeOptions="actionNext"
        android:nextFocusForward="@+id/hello2"/>
 
    <EditText
        android:id="@+id/hello2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/hello"
        android:text="Hello world 2"
        android:singleLine="true"
        android:imeOptions="actionNext"
        android:nextFocusForward="@+id/hello3"/>
 
    <EditText
        android:id="@+id/hello3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/hello"
        android:text="Hello world 3"
        android:singleLine="true" />
 
</RelativeLayout>

気付くまでに、結構時間を使ってしまった…

  1. 使用しているソフトウェアキーボードによりますが、大抵のものは出てくるはずです 
?

0 件のコメント: