The EditText use method of the Android control series _android

Source: Internet
Author: User
Tags gettext stub
Learning objectives:

1. How to establish EditText in Android
2, master the common properties of EditText
3, grasp the edittext focus of events, key events (listeners)

Introduced:

EditText is the most important control that accepts user input information. From the previous course, you might guess that you can use Edittext.gettext () to get its text, but in real projects, it might not be that simple, and you need more restrictions, such as the length of text, the number limit, and so on.

Given the limited screen size of your phone, you might want to save your control. Adding a title to the left of the text box that each user needs to fill out is an elegant way to go on the PC, but it's a waste on the phone, so in this case we'll learn how to use a edittext to achieve all the results:

Note that the first text box, the other controls just to test the effect of the transfer focus, we have the sample for the first text limit, when the user does not enter, it shows "Please enter your name" as a hint, in order to distinguish this is just a hint rather than the actual text, We use gray to differentiate, and when the user enters his or her content, the hint disappears and the font turns black, as shown below:

Finally we have a length limit on edittext, which can simply set its maxlength attribute in XML to a value, but the user can not enter more characters, there will be ambiguity, so you need to tell the user can not continue to enter the reason:

The XML layout code is as follows:

Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<edittext
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:textcolor= "#DDDDDD"
android:text= "Please enter your name"
Android:id = "@+id/et"
Android:maxlength = "10"
></EditText>
<button
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Click to change Focus"
></Button>
<edittext
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Click to change Focus"
Android:id = "@+id/et2"
></EditText>
</LinearLayout>

The background code is as follows:
Copy Code code as follows:

Indicates whether the text box is empty
Private Boolean IsEmpty = true;
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
EditText et = (edittext) This.findviewbyid (r.id.et);
Monitor focus Change event for control
Et.setonfocuschangelistener (New Onfocuschangelistener () {
@Override
public void Onfocuschange (View arg0, Boolean arg1) {
TODO auto-generated Method Stub
Gets the edittext that triggered the event
EditText Clickedittext = (edittext) arg0;
If you lose focus
if (arg1 = = False)
{
Get the current text
String text =clickedittext.gettext (). toString (). Trim ();
If you did enter the content artificially
if (Text.length () >0
&text.equals ("Please enter your name") = = False)
{
IsEmpty = false;
Clickedittext.settextcolor (Color.Black);
Clickedittext.settext (text);
}
Else
{
Clickedittext.settext ("Please enter your name");
Clickedittext.settextcolor (Color.gray);
IsEmpty = true;
}
}
If you get the focus
Else
{
Clickedittext.settextcolor (Color.Black);
If you are in an unedited state, empty the words "Please enter your name"
if (IsEmpty = = True)
{
Clickedittext.settext ("");
}
}
}
});
Listener control has new character input
Et.setonkeylistener (New Onkeylistener () {
@Override
public boolean OnKey (View arg0, int arg1, keyevent arg2) {
TODO auto-generated Method Stub
Gets the edittext that triggered the event
EditText Clickedittext = (edittext) arg0;
Get the current text
String text =clickedittext.gettext (). toString (). Trim ();
if (Text.length () ==10)
{
Prompt the user
Toast Toast = Toast.maketext (myactivity.this, "Maximum length is 10 characters", Toast.length_short);
Toast.setgravity (0, 0, 0);
Toast.show ();
}
return false;
}
});
}


Summary:
This article describes how to use EditText in Android, introduces common properties and events, and finally implements a user-experienced, and very common, text-limiting and prompting feature.

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.