Chapter 2 attracting your attention-UI programming (2), chapter 2 ui

Source: Internet
Author: User

Chapter 2 attracting your attention-UI programming (2), chapter 2 ui
2.1.3 text edit-edit box (EditText)

EditText is also a frequently used component in our development. For example, to implement a logon interface, you need to enter the account, password, email, and other information. Here, you need to use the EditText component to obtain the content entered by the user. below, let's take a logon interface as an example to see how EditText is used.

 

1) define a TextView (used to respond to button events) in the layout file, two EditText components (one used to record the user name, one used to record the password), one login button, and one cancel button, the 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 = "match_parent"

Android: layout_height = "match_parent">

<EditText

Android: id = "@ + id/name_edittext"

Android: layout_width = "200dp"

Android: layout_height = "wrap_content"

Android: hint = "Enter the user name"/>

<EditText

Android: id = "@ + id/pwd_edittext"

Android: layout_width = "200dp"

Android: layout_height = "wrap_content"

Android: password = "true"

Android: hint = "enter the password"/>

<LinearLayout

Android: orientation = "horizontal"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content">

<Button

Android: id = "@ + id/OK _button"

Android: layout_width = "80dp"

Android: layout_height = "wrap_content"

Android: text = "login"/>

<Button

Android: id = "@ + id/cancel_button"

Android: layout_width = "80dp"

Android: layout_height = "wrap_content"

Android: text = "cancel"/>

</LinearLayout>

</LinearLayout>

 

In EditText, the android: hint attribute is the default prompt information displayed when the user does not enter any content. Of course, we can also easily implement it in the code, for example:

EditText editView = (EditText) findViewById (R. id. name_edittext );

EditView. setHint ("Enter the user name ");

 

Android: password is used to enter confidential information such as passwords.

 

2) Click "Log on" to check whether the user name and password have been entered. If yes, "Logon successful" is displayed in TextView; otherwise, "Please check the input information" is displayed "; when you click "cancel", all text in TextView and EditText is cleared. Some code is as follows:

OkButton = (Button) findViewById (R. id. OK _button );

CancelButton = (Button) findViewById (R. id. cancel_button );

NameEditText = (EditText) findViewById (R. id. name_edittext );

PwdEditText = (EditText) findViewById (R. id. pwd_edittext );

StatusTextView = (TextView) findViewById (R. id. status_textview );

OkButton. setOnClickListener (new View. OnClickListener (){

@ Override

Public void onClick (View v ){

If (nameEditText. length () = 0 | pwdEditText. length () = 0 ){

StatusTextView. setText ("check input information ");

} Else {

StatusTextView. setText ("Logon successful ");

}

}

});

CancelButton. setOnClickListener (new View. OnClickListener (){

@ Override

Public void onClick (View v ){

NameEditText. setText ("");

PwdEditText. setText ("");

StatusTextView. setText ("");

}

});

 

Let's take a look at the effect, as shown in 2-6, 2-7, and 2-8:

Figure 2-6 initial Page

 

Figure 2-7 incomplete input information

 

Figure 2-8 logon successful

 

Experience Sharing:

Most of the time, the EditText component gets the focus by default and a soft keyboard is displayed, but we do not want this effect. In this case, you can add the following code to the layout file to avoid this situation:

<LinearLayout

Android: layout_width = "0dp"

Android: layout_height = "0dp"

Android: focusableInTouchMode = "true"

Android: focusable = "true"/>

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.