Step 4: menu events, common la S, radiobutton, checkbox usage, and toast usage

Source: Internet
Author: User

About menu events

When you click the menu button, oncreateoptionsmenu (menu) is called );

You can override the public Boolean oncreateoptionsmenu (menu) {} event.

For example:

Public Boolean oncreateoptionsmenu (menu ){

// Operate menu to add menu items

// The first parameter is the group ID of the menu.

// The second parameter is the ID of the current menu (similar to setting the item subscript)

// The third parameter is its sorting (not understood for the moment)

// The fourth parameter is the text of the button.

Menu. Add (0, 1, 1, R. String. Exit );

Menu. Add (0, 2, 2, R. String. About );

Return super. oncreatoptionsmenu (menu );

}

 

Menu event: When you click an item in the menu, onoptionsitemselected (menutitem item) is automatically called );

Override public Boolean onoptionsitemseleted (menuitem item) {} event

Example: (Continued example)

Public Boolean onoptionsitemseleted (menuitem item ){

// Obtain the ID of the item clicked by the user and execute the corresponding event

If (item. getitemid () = 1 ){

Finish (); // end the current activity (content in the previous chapter)

}

Return super. onoptionsitemselected (item );

}

 

Common Layout: the XML layout type of Android is similar to the CSS layout, and many CSS-like styles can be used for layout.

Android font size unit uses PT, such as Android: textsize = "35pt"; distance uses dip instead of PX, such as Android: padding = "10dip"; // better compatibility than PX pixels

Padding-padding margin-margin. If both controls have margin, the distance between the two controls is the addition of the margin values of the two controls.

 

Absolute layout:

Linearlayout and tablelayout are usually nested for layout. Pay attention to the use of Android: Orientation = ""; // you can specify whether the layout is horizontal or vertical.

Common attributes:

Android: grimace // specifies the control position, such as center and right (Note: Alignment of the control content with this attribute)

Android: sigleline // if it is true, the control content is displayed in the same row

Android: layout_weight = "1" // This attribute is very important. In multi-layer nesting, you need to set this attribute, which means a few points of each.

 

Relative layout:

Common attributes:

Basic Words:

Above/above

Below // below

Left // left

Right // right

The following attributes use "*" instead of the above four words. Basically, each attribute has these four directions, so they all have similar results.

// The following attributes are used in the "separation relationship". For example, a and B can only be "tangent" recently"

Android: Layout _ * // place the control with this attribute on the * plane of the control with the specified ID. (Note: This attribute does not mean that the two controls must be completely pasted together. If there is a little bit of contact, this attribute is satisfied)

For example, a control has this property -- Android: layout_above = "@ ID/B"; that is, a control is on top of B.

 

 

Basic Words:

Top // above

Bottom // below

Left // left

Right // right

Similarly, "*" is used to replace the four words.

// The following attributes are used in "inclusion relationship", for example, a contains B;

Android: layout_align * // associate the edge of the control that owns this property with the edge of the control of the specified ID, A small one contains a small one)

For example, control a has this property --- Android: layout_alignleft = "@ ID/B"; this means that the left edge of control a is aligned with that of control B.

 

// The following attribute uses the same word as the previous one.

Android: layout_alignparent * // if the property is true, the control is aligned with its parent control *

For example, the control has this property --- Android: layout_alignleft = "true"; this means that the alignleft of the a control is left aligned in the parent control.

 

 

Basic Words:

Horizontal // horizontal

Inparent // horizontal and vertical

Vertical // vertical

// Use "*" to replace the above three words

Android: layout_center * // if this attribute is true, the control is placed in the center of * of the parent control.

For example, control a has this property-Android: layout_centerhorizontal = "true"; meaning that control a is placed in the horizontal center of the parent control (horizontally centered)

 

 

How to Use radiobutton:

In use, like other controls, you need to add radiobutton in XML, but each group of radiobutton needs to be separated by radiogroup.

For example, a group of single-choice buttons-Male & female

<Radiogroup

Android: Id = "@ + ID/gendergroup"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: Orientation = "vertical" // determines whether the button is arranged horizontally or in the center.

>

<Radiobutton

Android: Id = "@ + ID/femalebutton"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "female"

/>

<Radiobutton

Android: Id = "@ + ID/malebutton"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "male"

/>

<Radiogroup>

 

Like other controls, you must use findviewbyid to obtain the radiogroup and all radiobutton contained in it;

When binding a listener, you need to use radiogroup to bind the listener. For example:

Gendergroup = (radiogroup) findviewbyid (R. Id. gendergroup );

Gendergroup. setoncheckedchangelistener ();

Listener interface: radiogroup. oncheckedchangelistener {}

Method of rewriting interface and click event of corresponding user:

Public void oncheckedchanged (radiogroup group, int checkedid ){

If (famalebutton. GETID () = checkedid ){

// Corresponding "when a user clicks the male button" Event

}

Else if (malebutton. GETID () = checkedid ){

// Corresponding "User click female" Event

}

}

 

 

Checkbox usage:

Similarly, the layout in XML is as follows:

<Checkbox

Android: Id = "@ + ID/run"

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "running"

/>

Runbox = (checkbox) findviewbyid (R. Id. Run );

Event binding: runbox. setoncheckedchangelistener ();

Listener type: compoundbutton. oncheckedchangelistener {}; // Note: You need to rewrite the corresponding event for each checkbox. Do not know if the existence of the listener does not respond to a set of checkboxes? Let's look back later.

Because each checkbox needs to be rewritten once, you can use a temporary variable to override it;

For example:

Runbox. setoncheckedchangelistener (New compoundbutton. oncheckedchangelistener {

Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked ){

If (ischecked ){

// Respond to the "user checked the 'run' checkbox" Event

}

Else {

// The user does not select

}

} // End for oncheckedchanged

} // End for checkbox listener Interface

); // End for control to bind the listener. Remember not to miss ";". It is best to finish setoncheckedchangelistener () first, and then plan to use the new interface in the slogan.

 

 

 

Toast usage // similar to the message on the object-oriented writing interface, the user prompts the user some errors, it seems that the button cannot be added? Coming back later

// The first parameter is the activity that owns the toast. Assume that the activity that owns the toast is called "test", and test. This indicates the activity object test.

// The second parameter is the text in the toast, that is, the content to be displayed.

// How is the third parameter displayed? Or? It's a constant. Let's use it like this. I'll see other usage later.

Toast. maketext (text. This, "hello", Toast. length_short );

 

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.