Android development tutorial-button checkbox edittext textview

Source: Internet
Author: User

Hi everyone!

Starting today, I will write frequently used controls in the android UI to help you learn these controls.

As an example, joke...

Mr. A found that his wife often had a stranger's text message on his cell phone, and the content of each text message was the same: "Zhao Xiong asked you to do something for me ." One night at half past ten, Mr. A grabbed the derailed wife and the man he was waiting for. He yelled at me: TMD. Do you think I can't understand the text message? It's "half past ten, I'll take off my bra "!

This section describes four controls: button checkbox edittext textview.

These four controls are frequently used and relatively simple controls.

I have compiled every control and comments here to help you get familiar with them. Each widget is an activity (form), such

SetProgramAfter running, the buttonactivity activity is displayed by default. The effect is as follows:

1 button

When the program runs, the page of the button control is displayed by default. You can see many buttons in the form. How can you define a button?

The idea is as follows: first create a layout file, then add the controls required for the form to the layout file, and then bind the layout file to the activity.CodeTo modify control properties and bind events...

With this idea, we will start to work.

1. Create a layout file in the res folder> layout folder of the project.

Right-click Create-> File

In the pop-up dialog box, enter the file name after the file name.

Button. xml

You can also create

Note: select other options

Note: Do not use special characters or uppercase letters when creating a name. Otherwise, you cannot create the layout file correctly ..

After the creation is successful, double-click to open the XML file and switch to the Code view.

Then, add a button tag in the middle of linearlayout, as shown below:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout
Xmlns: Android ="Http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<Button Android: Id = "@ + ID/btn1"

Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"

Android: text = "@ string/btnck"/>
</Linearlayout>

Meanings of colors:

RED: What is the control you created?

Blue: assign an ID to the created control, preferably a unique name. This ID will be in the R. the class ID class in Java generates a constant of the int type. As for its usefulness, if it has been programmed, we all know that we need to obtain these controls in the background to control them and modify attributes, obtain user operations. It will be discussed later in the Code if it is used.

Purple: the width and height of the widget.

Fill_parent: indicates that the control will overwrite the width defined by the parent class (if the width defined by the parent class is also fill_parent, it indicates the width and height of the parent class, which is the screen size ), the width of the control will fill the screen.

Match_parent: Like fill_parent above, it is full. The difference is that match_parent is available only in Versions later than Android.

Wrap_content: indicates the adaptive height, that is, how high the content is, Then how high it is displayed.

Yellow: indicates what text you will display in the control. Here we use the MVC mode, which is defined in string. xml and then applied here.

The first step is to reference the layout file in the activity.

By using the setcontentview method, you can bind the layout file we just defined to this form, and then run it directly.

After running, the control can be displayed normally, but there is no function, which obviously does not make much sense. The following is the code to control the control and bind events.

Remember the ID we set in XML just now?

Here, by using the btn1 = (button) findviewbyid (R. Id. btn1); method, you can obtain the button control you just defined and assign it to the btn1 object. Btn1 is a button object.

You can use the btn1 object to set some appearance attributes for this button, for example.

Bind event

This code first uses the setonclicklistener method and uses an anonymous internal class to implement a button click event. The intent and startactivity methods are introduced in my previous tutorials. If you forget them, you can check them out.

Here, we will introduce the button here. The button is mainly used to get the user's clicks, and then the corresponding processing is done in onclick.

2 checkbox

Let's take a look at what checkbox looks like and what it can do,

Is it the same as the checkbox in our HTML? Yes, it is actually a multi-choice button, the operation is relatively simple, directly paste the code

The same is true. Here I created a checkbox. xml layout file and an activity called checkboxactivity. java.

Then there is checkboxactivity. java, I first defined some global variables. If you need to use any type of controls in the form, first declare the control objects and then obtain them through the findviewbyid method, then, operate on these control objects. For example

First, bind the layout XML with the form, and then obtain the controls one by one.

You can set the appearance attribute or bind an event like the button mentioned above. Here I directly bind an option to the checkbox to change the event listener.

When the selected status of the check box changes, the oncheckedchanged function is triggered. You can use the checkbox1.ischecked () method to obtain the current status of this check box. True indicates the selected status, and false indicates the unselected status.

3 edittext

The edit box operation is also relatively simple, and you can directly paste the code

Run

4 textview is mainly used for text prompting

Same as above. I will not go into details here

I hope that my tutorial will help you a little and give you my shortcomings and suggestions. To help me do a better tutorial. Thank you!

Source codeYou have uploaded the file to the tianshengda website. You can download it: Beijing tianshengda Technology Co., Ltd.

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.