Android Click EditText edit box to clear hint preset word

Source: Internet
Author: User

Mainactivity:

/**
* After clicking on the EditText form, clear the preset word
* @author [email protected]
*
*/
public class Mainactivity extends activity{

@Override
protected void OnCreate (Bundle savedinstancestate) {
TODO auto-generated Method Stub
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
EditText username = (EditText) Findviewbyid (r.id.username);
EditText password = (EditText) Findviewbyid (R.id.password);
Button login = (button) Findviewbyid (R.id.login);

/*edittext default gets the cancellation of focus behavior?
The solution: In EditText's parent control,
Setting: Android:focusable= "true"
Android:focusableintouchmode= "true"
See the XML layout file. */

Username Focus Monitoring Events
Username.setonfocuschangelistener (New Onfocuschangelistener () {
public void Onfocuschange (View V, Boolean hasfocus) {
EditText et= (EditText) v;
if (!hasfocus) {//Lose focus
Et.sethint (Et.gettag (). toString ());
} else {
String hint=et.gethint (). toString ();
ET.SETTAG (hint);//Save the preset word
Et.sethint (NULL);
}
}
});
Password Focus Monitoring Events
Password.setonfocuschangelistener (New Onfocuschangelistener () {
public void Onfocuschange (View V, Boolean hasfocus) {
EditText et= (EditText) v;
if (!hasfocus) {//Lose focus
Et.sethint (Et.gettag (). toString ());
} else {
String hint=et.gethint (). toString ();
ET.SETTAG (hint);//Save the preset word
Et.sethint (NULL);
}
}
});

Login.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Toast.maketext (mainactivity.this, "login", Toast.length_long). Show ();
}
});
}
}

Layout interface:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:padding= "8DP"
Android:focusable= "true"
Android:focusableintouchmode= "true"
android:orientation= "Vertical" >
<edittext
Android:id= "@+id/username"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:hint= "Please enter your user name"/>
<edittext
Android:id= "@+id/password"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:hint= "Please enter your password"/>
<button
Android:id= "@+id/login"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:hint= "Login"/>

</LinearLayout>

Android Click EditText edit box to clear hint preset word

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.