Android – 在Spinner选择之后将焦点设置在EditText上

我在屏幕上显示以下项目:

EditText Forename
Spinner gender selection (Male | Female)
EditText Email

首次启动应用程序时,我希望在Forename EditText上设置焦点.然后在Spinner中选择“Male”或“Female”之后,
我希望焦点设置在微调器下方的Email EditText上.

我已经使用setOnItemSelectedListener在电子邮件EditText上设置requestFocus,但问题是它会自动设置焦点
每当我启动应用程序时,此EditText.
这是因为默认情况下,微调器显示第一个选择,在这种情况下是“男性”,因此它会思考
已经进行了选择并将焦点设置在“电子邮件”字段中.

我不介意默认情况下已经在微调器中选择了第一个选项但是如果我可以以某种方式覆盖要在其上设置的requestFocus
Forename EditText最初会很棒.

XML:

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

   <EditText
        android:id="@+id/ForenameForm"
        android:layout_width="285dp"
        android:layout_height="65dp"
        android:hint="@string/forenameHint"
        android:lines="1"
        android:singleLine="true"
        android:textSize="20sp" >
    </EditText>


   <Spinner  
        android:id="@+id/SpinnerGender" 
        android:spinnerMode="dialog"
        android:textSize="30sp" 
        android:layout_width="285dp"
        android:layout_height="60dp"
        android:prompt="@string/spinnerGender"  
        android:entries="@array/genderList">

    </Spinner>

   <EditText
        android:id="@+id/EmailForm"
        android:layout_width="285dp"
        android:layout_height="65dp"
        android:hint="@string/emailHint"
        android:lines="1"
        android:singleLine="true"
        android:textSize="20sp" >
    </EditText>

</LinearLayout>

活动类:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText forename=(EditText)findViewById(R.id.ForenameForm);
        forename.requestFocus();

        final EditText email=(EditText)findViewById(R.id.EmailForm);

        Spinner spinner=(Spinner)findViewById(R.id.SpinnerGender);
        spinner.setFocusable(true);
        spinner.setFocusableInTouchMode(true);   


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                email.requestFocus();               
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }               
        });  
    }
最佳答案
这个解决方案怎么样?

define a boolean flag and set it default false.

in oncreate set the focus of forname

in setOnItemSelectedListener 
     if flag is false then set flag true
     else focus email 

所以你的代码就像

boolean flag = false;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText forename=(EditText)findViewById(R.id.ForenameForm);
        forename.requestFocus();

        final EditText email=(EditText)findViewById(R.id.EmailForm);

        Spinner spinner=(Spinner)findViewById(R.id.SpinnerGender);
        spinner.setFocusable(true);
        spinner.setFocusableInTouchMode(true);   


        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
              // changes here
                if(flag == false)
                    flag = true;
                else 
                    email.requestFocus();               
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }               
        });  
    }

转载注明原文:Android – 在Spinner选择之后将焦点设置在EditText上 - 代码日志