9

在我的应用程序中,我有一个注册屏幕,有很多 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>

请为我提供触发微调器的最佳方法。

4

3 回答 3

7

在您的编辑文本上,覆盖 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:我看到您昨晚仍在为此苦苦挣扎,如果您没有找到解决方案(也没有发布解决方案)或帮助其他可能阅读此内容的人,这可能有助于从 spinner 获得编辑文本。

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) { }
    };
于 2012-12-04T03:55:28.320 回答
2

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

<Spinnner
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:nextFocusDown="@+id/youedittextid"
 />
于 2012-12-04T04:02:28.900 回答
0

除了上述专注于 Spinners 的解决方案之外,以下是 ime 选项的更多问题:

如果spinners介于 之间EditTexts,则位于忽略和属性EditTexts之下。在这两种情况下,它们都接受回车键并移至下一行,此时,它不应该允许回车键事件。spinnersimeOption="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;
    }
于 2018-07-09T13:23:13.367 回答