Android - set focus to EditText after selecting Spinner

The following items are displayed on the screen:

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

When the application starts for the first time, I want the focus to be set to Forename EditText. Then, after "Male" or "Female" was selected in "Spinner", I want the focus to be set to Email EditText, which is below the counter.

I used setOnItemSelectedListener to set requestFocus in an electronic EditText editor, but the problem is that it automatically sets focus to this EditText whenever I run the application. This is because by default the counter displays the first choice, which in this case is โ€œmasculineโ€, and therefore it thinks that the choice has already been made, and focuses on the โ€œEmailโ€ field.

I do not mind that the first choice was already selected in the counter by default, but if I could somehow redefine requestFocus , which will be set to Initially, Forename EditText would be great.

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> 

Action 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) { } }); } 
+6
source share
1 answer

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

Source: https://habr.com/ru/post/943482/


All Articles