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
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/mobile"
- android:layout_width="190DP"
- android:layout_height="wrap_content"
- android:text="mobile number" />
- <EditText
- android:id="@+id/address"
- android:layout_width="190DP"
- android:layout_height="wrap_content"
- android:text="Address" />
- </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
- Package com.genwoxue.onfocuchange;
- Import Android.os.Bundle;
- Import android.app.Activity;
- Import Android.view.View;
- Import Android.widget.EditText;
- Import Android.view.View.OnClickListener;
- Import Android.view.View.OnFocusChangeListener;
- Import Android.widget.Toast;
- Public class Mainactivity extends Activity {
- //Declaration EditText
- private EditText etmobile=null;
- private EditText etaddress=null;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- //Get EditText
- Etmobile= (EditText)Super.findviewbyid (r.id.mobile);
- Etaddress= (EditText)Super.findviewbyid (r.id.address);
- //Register onclick, Onfocuschange listener
- Etmobile.setonclicklistener (new Mobileonclicklistener ());
- Etmobile.setonfocuschangelistener (new Mobileonfocuschanagelistener ());
- Etaddress.setonclicklistener (new Addressonclicklistener ());
- Etaddress.setonfocuschangelistener (new Addressonfocuschanagelistener ());
- }
- //mobileonclicklistener Click Listener
- private class Mobileonclicklistener implements onclicklistener{
- @Override
- public void OnClick (view view) {
- Etmobile.settext ("");
- }
- }
- //mobileonfocuschanagelistener Focus Listener
- private class Mobileonfocuschanagelistener implements onfocuschangelistener{
- @Override
- public void Onfocuschange (View view,Boolean hasfocus) {
- if (View.getid () ==etmobile.getid ())
- Toast.maketext (Getapplicationcontext (), "Phone text box get focus!" ", Toast.length_long). Show ();
- }
- }
- //mobileonclicklistener Click Listener
- private class Addressonclicklistener implements onclicklistener{
- @Override
- public void OnClick (view view) {
- Etaddress.settext ("");
- }
- }
- //mobileonfocuschanagelistener Focus Listener
- private class Addressonfocuschanagelistener implements onfocuschangelistener{
- @Override
- public void Onfocuschange (View view,Boolean hasfocus) {
- if (View.getid () ==etaddress.getid ())
- Toast.maketext (Getapplicationcontext (), "Address text box get focus!" ", Toast.length_long). Show ();
- }
- }
- }
2. The final effect is as follows:
(Get Focus)
(Click to clear the text)