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