Android:EditText NextFocusDown不会触发Spinner

时间:2012-12-04 03:48:37

标签: android android-layout android-edittext android-spinner

在我的应用程序中,我有一个注册屏幕,有很多EditTexts和Spinners。 我想逐一浏览“注册”字段。

所以我申请了android:imeOptions="actionNext"。但它忽略了所有的Spinners。它将仅关注EditText。我也试过setNextFocusDownId()。这也忽略了微调器。

   <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/reportentry11"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/numbers"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:imeOptions="actionNext"
            android:inputType="phone" 
            >
           <requestFocus/>
        </EditText>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reportentry12"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >

            <TextView
                android:id="@+id/txt_exp"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"

                android:text="EXP:"
                android:textColor="#000000"
                android:textSize="12dp" />

            <Spinner
                android:id="@+id/spin_date"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="3"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:nextFocusDown="@+id/spin_year"
                android:text="date"
                android:textSize="12dp" />

            <Spinner
                android:id="@+id/spin_year"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginRight="5dp"

                  android:nextFocusDown="@+id/cvc"
                android:text="year"
                android:textSize="12dp" />
        </LinearLayout>

        <EditText
            android:id="@+id/cvc"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:hint="@string/reg_label_cvc"
            android:imeOptions="actionNext"
            android:inputType="phone" />

        <EditText
            android:id="@+id/fname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/reg_label_fname"
            android:imeOptions="actionNext" />

        <EditText
            android:id="@+id/address"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/reg_label_address"
            android:imeOptions="actionNext" />

        <EditText
            android:id="@+id/city"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/reg_label_city"
            android:imeOptions="actionNext"
            android:nextFocusDown="@+id/pr_spin" />

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/reportentry13"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/txt_pr"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="PROV:"
                android:textColor="#000000"
                android:textSize="12dp" />

            <Spinner
                android:id="@+id/pr_spin"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="date"
                android:imeOptions="actionNext"
                android:textSize="14dp" />

            <EditText
                android:id="@+id/pcode"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:hint="@string/reg_label_pcode"
                android:imeOptions="actionDone" />
        </LinearLayout>

        <Button
            android:id="@+id/register_register_button"
            android:layout_width="wrap_content"
            android:background="@drawable/green_button_bg"
            android:onClick="completeClicked"
            android:text="@string/reg_label_complete"
            android:textSize="28dp"
            android:textStyle="bold" />
    </LinearLayout>

请告诉我触发旋转器的最佳方法。

3 个答案:

答案 0 :(得分:4)

在你的编辑文本上,覆盖onEditorAction并给予焦点(或做任何像打开你的微调器那样的事情)......

yourEditTXT.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView view, int actionID, KeyEvent event) {
            if (actionID == EditorInfo.IME_ACTION_NEXT) {
                //do your stuff here...
                return true;
            }
            return false; 
    }
});     

编辑12/4: 我发现你昨晚仍然在努力解决这个问题,如果你没有找到解决方案(并没有发布一个解决方案)或帮助其他人阅读本文,这可能有助于从微调器中获取编辑文本。 / p>

mySpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());            

public class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parentview, View v, int position, long id) {
            // your spinner proces code 
            // then when you are done, 
            yourEditText.setFocusableInTouchMode(true);  //if this is not already set
            yourEditText.requestFocus();  //to move the cursor
            final InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);  // this line and the one above will open the soft keyboard if it doesn't already open
        }

        public void onNothingSelected(AdapterView<?> arg0) { }
    };

答案 1 :(得分:2)

在xml文件中执行以下操作:

<Spinnner
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:nextFocusDown="@+id/youedittextid"
 />

答案 2 :(得分:0)

除了上面的解决方案集中于Spinners之外,以下还有ime选项的麻烦:

如果spinnersEditTexts之间,则位于EditTexts下的spinners会忽略imeOption="actionDone"lines="1"属性。在这两种情况下,他们都接受回车键并移至下一行,这时,它们不应允许回车键事件。

我发现,尽管ime的侦听器仍然完好无损,但ime选项设置为任何值(下一步或完成),但将其更改为0。

因此,我的答案是第二部分,而我仍在尝试使微调器更具针对性:

xml:

     <EditText
                android:id="@+id/et_1"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_40"
                android:layout_marginLeft="@dimen/dp_8"
                android:layout_marginStart="@dimen/dp_8"
                android:gravity="start|bottom"
                android:hint="@string/et_1"
                android:imeOptions="actionNext"
                android:inputType="textCapWords" />
<!--This ime option works fine-->
<!--lots of spinners in between these Edit texts-->

                <EditText
                android:id="@+id/et_2"
                android:layout_gravity="start|bottom"
                android:layout_marginLeft="@dimen/dp_8"
                android:layout_marginStart="@dimen/dp_8"
                android:gravity="start|bottom"
                android:hint="@string/et_2"
                android:imeOptions="actionDone" />
<!--this actionDone is ignored and set to 0-->

[actionNext的值= 5,而actionDone的值= 0,但对于编辑文本et2在侦听器中报告为0] 设置监听器:

...
        EditText et_1 = view.findViewById(R.id.et_1);
        fet_1.setOnEditorActionListener(this);
        //==
        EditText et_2 = view.findViewById(R.id.et_2);
        et_2.setOnEditorActionListener(this);
...

处理ime动作也被忽略的侦听器:

@Override
    public boolean onEditorAction(TextView textView, int actionID, KeyEvent keyEvent) {
        FontEditText et_1 = getView().findViewById(R.id.et_1);
        //==
        EditText et_2 = getView().findViewById(R.id.et_2);
        final int id = textView.getId();
        switch (id) {
            case R.id.et_1: {
                if (actionID == EditorInfo.IME_ACTION_NEXT) {
                    // works actionID = 5.
//inherited method
                    activity.hideKeyboard(textView);
                    fet_bus_entity.clearFocus();
                    et_business_add.requestFocus();
                    et_business_add.performClick();
                    return true;
                }
            }
            break;
            case R.id.et_2: {
//following line doesn't work hence commented, value of actionID = 0 here.
//                if (actionID == EditorInfo.IME_ACTION_DONE) {

                    et_business_add.clearFocus();
//inherited method
                    activity.hideKeyboard(textView);
                    return true;
//                }

            }
//            break;
        }
        return false;
    }