Reasons and solutions for Requestfocus () invalidation in Android development _android

Source: Internet
Author: User

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

SetVisible (TRUE); 

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.