23rd, Onfocuchangelistener Focus event (learn Android from scratch)

Source: Internet
Author: User

In Android apps, Onfocuchangelistener focus events are essential, and we'll learn how to do it on the basis of the previous chapter.

Basics: Onfocuchangelistener Events

First, the interface

Open the "res/layout/activity_main.xml" file.

1. Drag the 2 edit box EditText from the toolbar to the activity, respectively. The control is from form Widgets.

  

2. Open the Activity_main.xml file.

[HTML]View Plaincopy
  1. <LinearLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <EditText
  7. android:id="@+id/mobile"
  8. android:layout_width="190DP"
  9. android:layout_height="wrap_content"
  10. android:text="mobile number" />
  11. <EditText
  12. android:id="@+id/address"
  13. android:layout_width="190DP"
  14. android:layout_height="wrap_content"
  15. android:text="Address" />
  16. </linearlayout>
3, the interface is as follows

  

Ii. Onitemselectedlistener Events

1. Open the "Src/com.genwoxue.onfocuchange/mainactivity.java" file.

Then enter the following code:

  

[Java]View Plaincopy
  1. Package com.genwoxue.onfocuchange;
  2. Import Android.os.Bundle;
  3. Import android.app.Activity;
  4. Import Android.view.View;
  5. Import Android.widget.EditText;
  6. Import Android.view.View.OnClickListener;
  7. Import Android.view.View.OnFocusChangeListener;
  8. Import Android.widget.Toast;
  9. Public class Mainactivity extends Activity {
  10. //Declaration EditText
  11. private EditText etmobile=null;
  12. private EditText etaddress=null;
  13. @Override
  14. protected void OnCreate (Bundle savedinstancestate) {
  15. super.oncreate (savedinstancestate);
  16. Setcontentview (R.layout.activity_main);
  17. //Get EditText
  18. Etmobile= (EditText)Super.findviewbyid (r.id.mobile);
  19. Etaddress= (EditText)Super.findviewbyid (r.id.address);
  20. //Register onclick, Onfocuschange listener
  21. Etmobile.setonclicklistener (new Mobileonclicklistener ());
  22. Etmobile.setonfocuschangelistener (new Mobileonfocuschanagelistener ());
  23. Etaddress.setonclicklistener (new Addressonclicklistener ());
  24. Etaddress.setonfocuschangelistener (new Addressonfocuschanagelistener ());
  25. }
  26. //mobileonclicklistener Click Listener
  27. private class Mobileonclicklistener implements onclicklistener{
  28. @Override
  29. public void OnClick (view view) {
  30. Etmobile.settext ("");
  31. }
  32. }
  33. //mobileonfocuschanagelistener Focus Listener
  34. private class Mobileonfocuschanagelistener implements onfocuschangelistener{
  35. @Override
  36. public void Onfocuschange (View view,Boolean hasfocus) {
  37. if (View.getid () ==etmobile.getid ())
  38. Toast.maketext (Getapplicationcontext (), "Phone text box get focus!"  ", Toast.length_long). Show ();
  39. }
  40. }
  41. //mobileonclicklistener Click Listener
  42. private class Addressonclicklistener implements onclicklistener{
  43. @Override
  44. public void OnClick (view view) {
  45. Etaddress.settext ("");
  46. }
  47. }
  48. //mobileonfocuschanagelistener Focus Listener
  49. private class Addressonfocuschanagelistener implements onfocuschangelistener{
  50. @Override
  51. public void Onfocuschange (View view,Boolean hasfocus) {
  52. if (View.getid () ==etaddress.getid ())
  53. Toast.maketext (Getapplicationcontext (), "Address text box get focus!"  ", Toast.length_long). Show ();
  54. }
  55. }
  56. }


2. The final effect is as follows:

(Get Focus)

  

(Click to clear the text)

  

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.