Objective
Recently do the company project, often encounter a problem, is that I for a control such as EditText set Requestfocus () when the use, for example, when landing, I judge the user entered the password, if the correct landing, Error prompts password error, and the input box to get focus, But it actually doesn't work.
Package Com.example.hfs.requestfocusdemo;
Import android.content.Intent;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.Toast; public class Mainactivity extends Appcompatactivity implements View.onclicklistener {private EditText Musername,mpasswo
Rd
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
private void Initview () {musername= (edittext) This.findviewbyid (r.id.et_username);
Mpassword= (EditText) This.findviewbyid (R.id.et_password);
Mpassword.setonclicklistener (this); @Override public void OnClick View {if (!)
123456 ". Equals (Mpassword.gettext (). toString ())) {Toast.maketext (this," Password is incorrect, please re-enter ", Toast.length_short). Show ();
Mpassword.requestfocus (); }else{startactivity (New Intent (This,mainactivity.class));
Toast.maketext (This, "Landing successful", Toast.length_short). Show ();
Finish (); }
}
}
Reason
Check the data analysis, may be the following questions:
Android load Refresh UI when it is from left to right, from top to bottom in the order, is loading the process, if Requestfocus (), then, it is possible at this time not to refresh the entire interface, resulting in requestfocus invalid.
Solutions
After finding out the reason, there is a solution, give him a delay time:
Mpassword.postdelayed (New Runnable () {
@Override public
void Run () {
mpassword.requestfocus ();
}
This solves the problem.
Ps:requestfocus () Reason for invalid
This means that requestfocus () should be placed after setvisible (true).
The above is a small set to introduce the Android development of Requestfocus () invalid reasons and solutions, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!