安卓:EditText的NextFocusDown不触发Spinner

9
在我的应用程序中,我有一个注册屏幕,有很多EditText和Spinners。我想逐个浏览注册字段。因此,我应用了android:imeOptions="actionNext"。但它忽略了所有的Spinners。它只会将焦点放在EditText上。我还尝试过setNextFocusDownId(),但这也忽略了Spinners。
   <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>

请为我提供最佳的启动旋转器方式。

我认为这个链接会对你有所帮助... link谢谢... - user4232
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转换到EditText。

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) { }
    };

它正在工作,但是弹出式旋转列表没有显示,只有旋转器被突出显示。 - Sridhar
@GMRamesh所说的加上yourWidget.performClick()应该就可以了。 - logray
您用于将EditText转换为Spinner的代码有效,现在请提供将Spinner转换为EditText的代码。 - Sridhar
@sridhar,我已经编辑了答案,请检查并告诉我...你必须在那里添加下一个EditText ID。 - G M Ramesh

2
在您的 XML 文件中执行以下操作:
<Spinnner
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:nextFocusDown="@+id/youedittextid"
 />

它对我来说是有效的...请仔细检查您的代码并进行相应更改...如果您复制粘贴上面的答案,它并不会像那样工作...您必须根据自己的情况进行更改。 - G M Ramesh
@sridhar,你的代码中有两个Spinner在一个LinearLayout中,所以你试图去访问不在同一LinearLayout中的EditText...我建议你只使用一个RelativeLayout并检查它是否有效。 - G M Ramesh
你的Spinner android:nextFocusDown="@+id/spin_year" --> android:id="@+id/spin_year" 在同一个LinearLayout中是否正常工作... 如果它能正常工作,那么对于EditText也应该百分之百正常工作。 - G M Ramesh
@Sridhar和GM,我看到你们没有发布任何更新,所以我编辑了下面的帖子,希望你们觉得有用,如果你们没有找到解决方案的话。另外,请更新这个问题的解决方案,如果你们找到了除昨晚之外的其他解决方案。 - logray

0
除了以上集中解决Spinner的方案外,以下是与ime选项相关的进一步问题:
如果Spinner位于EditText之间,则位于Spinner下方的EditText会忽略imeOption="actionDone"和lines="1"属性。在这两种情况下,它们都接受回车键并移动到下一行,而实际上不应该允许回车键事件。
我发现尽管ime的监听器仍然完好无损,但设置为任何值(下一个或完成),它都会变成0。
因此,我的答案是针对第二部分,同时我仍在努力使Spinner可聚焦:
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,但是在监听器中,edit text 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);
...

处理监听器时也忽略了输入法操作:

@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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接