Chapter two attracts your eyeballs.-ui Programming (2)

Source: Internet
Author: User

2.1.3 Text Edit-edit box (EditText)

EditText is also a frequently used component in our development. For example, to implement a login interface, users need to enter the account number, password, mail and other information, here you need to use the EditText component to obtain user input content, below, we take a login interface as an example, see how edittext is used.

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

<edittext

Android:id= "@+id/pwd_edittext"

Android:layout_width= "200DP"

android:layout_height= "Wrap_content"

Android:password= "true"

Android:hint= "Please enter your 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>

EditText in Android:hint This property is a prompt that is displayed by default when the user does not enter any content. Of course, we can also easily implement it in code, such as:

EditText EditView = (EditText) Findviewbyid (R.id.name_edittext);

Editview.sethint ("Please enter user name");

And Android:password This property is used to enter the password, such as privacy needs to be confidential information.

2) Click on the "Login" button to determine whether the user name and password have been entered, if it is displayed in the TextView "login Success", otherwise display "Please check the input information", click "Cancel" button, the text in TextView and EditText empty, part of the 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 ("Please check the input information");

}else{

Statustextview.settext ("Login 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 results, 2-6, 2-7, 2-8:

Figure 2-6 Initial interface

Figure 2-7 Input information is incomplete

Figure 2-8 Login Success

Experience Sharing:

Most of the time, the EditText component will get focus by default and pop up the soft keyboard, but we don't want this effect. At this point, you can include the following code in the layout file to avoid this scenario:

<linearlayout

Android:layout_width= "0DP"

android:layout_height= "0DP"

Android:focusableintouchmode= "true"

Android:focusable= "true"/>

Chapter two attracts your eyeballs.-ui Programming (2)

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.