The 13th chapter: on the common control

Source: Internet
Author: User

One, nonsense

Today we will present the declarations and event responses of the Textview,button,edittext,radiobutton,checkbox,togglebutton,ratingbutton Seven controls in the common controls that the Android system provides for us.

Second, the text

1, TextView

Similar to the Label control in ASP. NET, the read-only display control can be set by GetText () with its Android:text property, SetText () setting its Android:text property. Add the following code to declare TextView in the LinearLayout section of Res/layout/main.xml.

<android:layout_width= "Fill_parent"  android:layout_height= "Wrap _content "  android:text=" @string/hello "  android:id=" @+id/mytextview " />

The control can be obtained in Java code through the following code.

// Get the control TextView Mytextview = (TextView) Findviewbyid (R.id.mytextview);

2. Button

A button control, through which a user submits his or her request, is one of the most common controls that a user interacts with an application.

Declaring a control in Res/layout/main.xml

<android:layout_width= "Wrap_content"  android:layout_height= " Wrap_content "  android:id=" @+id/mybutton "/>

So how do you respond to a user's actions?

// Response button's Click event                  = (Button) Findviewbyid (R.id.mybutton);        Mybutton.setonclicklistener (new  Onclicklistener () {            @Override            public void OnClick (View v) {                //  TODO auto-generated method stub                toast.maketext ( Android_control_demoactivity.  This ,                        " Click button ", Toast.length_short). Show ();            }        });

3, EditText

The Android system provides a text box for user input, such as a textbox in ASP.

Add the EditText label to the Res/layout/main.xml.

<android:layout_width= "Fill_parent"  android:layout_height= "Wrap _content "  android:id=" @+id/myedittext "/>

Let's edittext a carriage return action, see the code below, which shows how to get the value of EditText and how to assign a value to TextView.

//manipulating EditText controls, fetching values, and responding to eventsMyedittext =(EditText) Findviewbyid (R.id.myedittext); Myedittext.setonkeylistener (NewOnkeylistener () {@Override Public BooleanOnKey (View V,intKeyCode, KeyEvent event) {                //In response to the user's return action, the EditText value is displayed in the TextView                if((event.getaction () = = Keyevent.action_down) && (keycode = =keyevent.keycode_enter))                      {Mytextview.settext (Myedittext.gettext ()); return true; }                    return false; }        });

4, RadioButton

Radio button, placed in a radiogroup, only one option can be selected in this group, such as when you choose Gender, you can only select one gender.

Declare the control in Layout/main.xml.

<RadiogroupAndroid:id= "@+id/radiogroup1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content">        <RadioButtonAndroid:id= "@+id/radio0"Android:layout_width= "Wrap_content"Android:text= "Red"Android:layout_height= "Wrap_content"android:checked= "true"></RadioButton>        <RadioButtonAndroid:id= "@+id/radio1"Android:layout_width= "Wrap_content"Android:text= "Blue"Android:layout_height= "Wrap_content"></RadioButton>        <RadioButtonAndroid:id= "@+id/radio2"Android:layout_width= "Wrap_content"Android:text= "Green"Android:layout_height= "Wrap_content"></RadioButton>    </Radiogroup>

Response RadioButton

Myradiogroup.setoncheckedchangelistener (NewOncheckedchangelistener () {@Override Public voidOnCheckedChanged (Radiogroup Group,intCheckedid) {                //TODO auto-generated Method Stub                if(Radio_1.getid () = =Checkedid) {Toast.maketext (android_control_demoactivity). This, Radio_1.gettext (), Toast.length_short). Show (); }                if(Radio_2.getid () = =Checkedid) {Toast.maketext (android_control_demoactivity). This, Radio_2.gettext (), Toast.length_short). Show (); }                if(Radio_3.getid () = =Checkedid) {Toast.maketext (android_control_demoactivity). This, Radio_3.gettext (), Toast.length_short). Show (); }            }        });

5. CheckBox

Check button. In the application if you let the user choose their own preferences, because the user's preferences can not be only one, so you must use the Check button, allowing users to choose multiple.

Declare the control in Res/layout/main.xml.

<android:text= "checkbox"  android:id= "@+id/checkbox1"  Android:layout_width= "Wrap_content"  android:layout_height= "Wrap_content"  ></CheckBox>

The following is a response to the user's selection action, and typically, the check box is selected with a button to uniformly commit, but in the example we only show how to respond to the change event of the checkbox.

//to bind a response event for a Mychecbox controlMyCheckBox =(CheckBox) Findviewbyid (r.id.checkbox1); Mycheckbox.setoncheckedchangelistener (NewOncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {                //TODO auto-generated Method Stub                if(isChecked) {toast.maketext (android_control_demoactivity. This, Mycheckbox.gettext (), Toast.length_short). Show (); }            }        });

6. Toggle Button

If anyone who has ever had an Android system knows that when you turn WiFi on and off, there is a button that appears on when you turn on WiFi and the button shows off when you turn off WiFi, which only has these two states. This button is ToggleButton.

Defining controls in Res/layout/main.xml

<android:text= "ToggleButton"  android:id= "@+id/togglebutton1"  android:layout_width= "Wrap_content"  android:layout_height= "Wrap_content"  Android:texton= "on"  android:textoff= "Off"></ ToggleButton >

Checked event in response to toggle button

//responding to Mytogglebutton checked eventsMytogglebutton =(ToggleButton) Findviewbyid (R.id.togglebutton1); Mytogglebutton.setoncheckedchangelistener (NewOncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) {                //TODO auto-generated Method Stub                if(isChecked) {toast.maketext (android_control_demoactivity. This, mytogglebutton.ischecked () + "", Toast.length_short). Show (); }            }        });

7, Rating Bar

Scoring bar. It is possible to define his growth rate and maximum value according to our needs.

Declaring a control in Res/layout/main.xml

<android:id= "@+id/ratingbar1"  android:layout_width= "Wrap_ Content "  android:layout_height=" Wrap_content "  android:numstars=" 5 "  Android:stepsize= "1.0"></ratingbar>

android:numstarts--number of Pentagram in the scoring bar

android:stepsize--that is you click once, will select a star of how much, if it is 1.0, then click Once is a star, if set to 0.5 then is half star.

Response Nyratingbar Ratingbarchange Event        Myratingbar = (ratingbar) Findviewbyid (R.ID.RATINGBAR1);        Myratingbar.setonratingbarchangelistener (New Onratingbarchangelistener () {            @Override public            Void Onratingchanged (Ratingbar Ratingbar, float rating,                    boolean fromuser) {                //TODO auto-generated method stub                Toast.maketext (Android_control_demoactivity.this, "New Rating:" + Rating, Toast.length_short). Show ();            }        );

8, the last to a

The 13th chapter: on the common control

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.