Android -在Spinner选择后集中于EditText。

时间:2023-01-21 14:47:33

I have the following items displayed on the screen:

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

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

When the application is first launched, I want the focus to be set on the Forename EditText. Then after "Male" or "Female" has been selected in the Spinner, I want the focus to be set on the Email EditText that is located below the spinner.

当应用程序第一次启动时,我希望将焦点放在前面的EditText上。然后,在微调器中选择“男性”或“女性”之后,我希望将焦点设置在位于微调器下方的电子邮件EditText上。

I have used setOnItemSelectedListener to set the requestFocus on the email EditText, but the problem is that it automatically sets the focus on this EditText whenever I launch the application. This happens because by default the spinner displays the first selection which in this case is "Male" and therefore it thinks that a selection has already been made and sets the focus on the Email field.

我已经使用setOnItemSelectedListener将requestFocus设置为email EditText,但问题是每当我启动应用程序时,它都会自动将焦点设置在这个EditText上。这是因为默认情况下,spinner显示第一个选择,在本例中为“Male”,因此它认为已经进行了一个选择,并将焦点放在电子邮件字段上。

I don't mind the first selection to be already selected in the spinner by default but if I could somehow override the requestFocus to be set on the Forename EditText initially that would be great.

默认情况下,我不介意spinner中已经选择的第一个选项,但是如果我能以某种方式覆盖requestFocus,使其在初始时设置为在用户名EditText上,那就太好了。

XML:

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>

Activity class:

活动类:

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

            }               
        });  
    }

1 个解决方案

#1


7  

How about this solution.

这个解决方案。

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 

So your code will be like

所以你的代码会是

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

            }               
        });  
    }

#1


7  

How about this solution.

这个解决方案。

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 

So your code will be like

所以你的代码会是

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

            }               
        });  
    }