How to Use EditText of Android control series

Source: Internet
Author: User

Objective:

1. Master how to create EditText in Android
2. Master common attributes of EditText
3. Master EditText focus events and button events (listener)

Introduction:

EditText is the most important control for receiving user input information. After learning the previous course, you may guess that you can use EditText. getText () is used to obtain its text. However, in a real project, it may be less simple and requires more restrictions, such as text length restrictions and numeric restrictions.

Given the limited cell phone screen size, you may always think about how to save controls. Adding the title on the left of the text box that each user needs to fill in on the PC is an elegant method, but it will be a waste on the mobile phone, therefore, in this example, we will learn how to use an EditText to achieve all the effects:

Note that in the first text box, other controls are only used to test the effect after the focus is transferred. For example, when the first text is not entered, it displays "enter your name" as a prompt. to distinguish this from a prompt rather than a real text, we use Gray to differentiate it. When users enter their own content, this prompt will disappear and the font turns black, for example:

At last, we set the length limit for EditText. This can be used to set the maxLength attribute to a value in XML. However, ambiguity may occur when users cannot enter more characters, therefore, you must inform the user of the reason why the input cannot be continued:

The XML layout code is as follows:

Copy codeThe Code is 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 = "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 codeThe Code is as follows: // indicates whether the text box is empty.
Private Boolean isEmpty = true;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
EditText et = (EditText) this. findViewById (R. id. et );
// Listens to the focus change event of the control.
Et. setOnFocusChangeListener (new OnFocusChangeListener (){
@ Override
Public void onFocusChange (View arg0, boolean arg1 ){
// TODO Auto-generated method stub
// Obtain the EditText of the trigger event
EditText clickEditText = (EditText) arg0;
// If the focus is lost
If (arg1 = false)
{
// Obtain the current text
String text = clickEditText. getText (). toString (). trim ();
// If the content is input manually
If (text. length ()> 0
& Text. equals ("enter your name") = false)
{
IsEmpty = false;
ClickEditText. setTextColor (Color. BLACK );
ClickEditText. setText (text );
}
Else
{
ClickEditText. setText ("enter your name ");
ClickEditText. setTextColor (Color. GRAY );
IsEmpty = true;
}
}
// If the focus is obtained
Else
{
ClickEditText. setTextColor (Color. BLACK );
// If it is not edited, enter the words "enter your name ".
If (isEmpty = true)
{
ClickEditText. setText ("");
}
}
}
});
// The 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
// Obtain the EditText of the trigger event
EditText clickEditText = (EditText) arg0;
// Obtain the current text
String text = clickEditText. getText (). toString (). trim ();
If (text. length () = 10)
{
// Prompt the user
Toast toast = Toast. makeText (MyActivity. this, "the 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, describes common attributes and events, and finally implements a user experience that is very common for text restrictions and tips.

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.