Android Input Control Classic-input controls "pack"

Source: Internet
Author: User
Tags call back

The input control is a class of interactive components of the application user interface. The Android system provides a number of input controls that you can use in the UI, such as buttons, text editing spaces, checkboxes, radio boxes, and various dialog boxes.

Basic input Controls

Below we explain the basic usage of the input control through an example of a personal Settings page.

Look at the interface effect first, as shown in 10-6.

▲ Figure 10-6 Control Sample Interface

Main interface Main.xml

1. <?xml version= "1.0" encoding= "Utf-8"?>

2. <linearlayout

3. xmlns:android= "Http://schemas.android.com/apk/res/android"

4. Android:layout_width= "Fill_parent"

5. android:layout_height= "Fill_parent"

6. android:orientation= "Vertical"

7. Android:background= "#ffe8e8e8" >

8. <linearlayout

9. Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content" >

<textview.

Android:id= "@+id/textview1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "Avatar:"

android:layout_gravity= "Center"

Android:textcolor= "#000000"/>

<imageview.

Android:id= "@+id/imageview1"

Android:layout_width= "100DP"

android:layout_height= "100DP"

android:src= "@drawable/sample"/>

</LinearLayout>

<linearlayout.

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content" >

<textview.

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:textcolor= "#000000"

android:text= "Name:"/>

<edittext.

Android:id= "@+id/edittext1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:layout_weight= "1" >

Panax <requestfocus/>

</EditText>.

</LinearLayout>

<linearlayout.

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content" >

<textview.

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "Gender:"

Android:textcolor= "#000000"

android:layout_gravity= "Center"/>

<radiogroup.

Android:id= "@+id/radiogroup1"

Wuyi android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:orientation= "Horizontal" >

<radiobutton.

Android:id= "@+id/radiobutton1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:textcolor= "#000000"

android:text= "Male"

Android:checked= "true"/>

<radiobutton.

Android:id= "@+id/radiobutton2"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:textcolor= "#000000"

android:text= "female"/>

</RadioGroup>

</LinearLayout>

<linearlayout.

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content" >

<textview.

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:text= "Hobby:"

Android:textcolor= "#000000"

android:layout_gravity= "Center"/>

<checkbox android:id= "@+id/checkbox1"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Bayi. android:textcolor= "#000000"

android:text= "singing"/>

<checkbox android:id= "@+id/checkbox2"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

Android:textcolor= "#000000"

android:text= "Dancing"/>

</LinearLayout>.

<linearlayout.

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content" >

<textview.

Android:layout_width= "Wrap_content"

94. android:layout_height= "Wrap_content"

android:text= "Marriage:"

Android:textcolor= "#000000"

android:layout_gravity= "Center"/>

98. <togglebutton android:id= "@+id/togglebutton1"

Android:texton= "Married"

android:textoff= "Unmarried"

101. Android:layout_width= "Wrap_content"

102. android:layout_height= "Wrap_content"/>

103. </LinearLayout>

104. <linearlayout

Android:layout_width= "Fill_parent"

106. Android:layout_height= "Wrap_content" >

107. <textview

108. Android:layout_width= "Wrap_content"

109. android:layout_height= "Wrap_content"

android:layout_gravity= "Center"

111. android:text= "Birthday:"

Android:textcolor= "#000000"/>

113. <datepicker android:id= "@+id/datepicker"

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:layout_gravity= "Center_horizontal"/>

117. </LinearLayout>

118. <linearlayout

119. Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content" >

121. <textview

122. Android:layout_width= "Wrap_content"

123. android:layout_height= "Wrap_content"

124. android:text= "From:"

Android:textcolor= "#000000"/>

126. <spinner

127. Android:id= "@+id/spinner1"

android:text= "Domestic"

129. Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

131./>

</LinearLayout>

133. <button android:id= "@+id/button1"

134. android:layout_height= "Wrap_content"

135. Android:layout_width= "Fill_parent"

136. android:text= "Normal button"/>

137. </LinearLayout>

Main interface layout We adopt vertical linear layout in the whole, nested horizontal linear layout.

We use TextView to do the text display, set the text content we want to show through the android:text= "" property, where the Android:textcolor property sets the font color.

Line 18th to 22nd is a imageview used to display the image, where we specify an image with a display size of 100DP, set the picture to be displayed by the Android:src property. Of course we can also set the image to be displayed in the Java code.

The EditText control in line 32nd to 38th is often used as a text input box, setting the <requestfocus/> property to get input focus. We can also set the Android:inputtype property to limit the type of input to numbers, phone number, password, IP address, and so on.

Line 49th to 67th declares that the 2 RadioButton radio buttons within the Radiogroup tab are combined into a single box. The listener is set up as a whole by Radiogroup. The Android:checked property is used to set the default value.

Line 78th to 87th is a multi-box of 2 checkboxes, which must be different from the radio box, and the checkbox can be placed independently, setting the listener independently.

Line 98th to 103th ToggleButton is a button in the form of a switch that sets the text of the selected state through the Android:texton property and sets the unselected text through the Android:textoff property. Functionally, it is similar to a checkbox.

The 113th to 116th line of DatePicker is a date selection control that can be selected for the year, month, day, or soft keyboard to enter the specified date and month.

The spinner control on line 126th to 131th is a list selection box that pops up a selection list and allows the user to select a value from a set of data. The specific content within the control is done by setting the adapter in the code.

Line 133th to 136th is a normal button, and we can set the button's listener event in the code to complete the operation.

The following is the Java Code section of this example.

Testactivity.java

1. Public class Testactivity extends Activity {

2. Private ImageView ImageView1;

3. Private EditText EditText1;

4. Private Spinner Spinner1;

5. Private Radiogroup RadioGroup1;

6. Private CheckBox Checkbox1,checkbox2;

7. Private ToggleButton ToggleButton1;

8. Private Arrayadapter<string> Adapter;

9. Private DatePicker DatePicker;

Button1 private Button;

@Override.

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (r.layout.test);

Findwidget ();}

. void Findwidget ()

17. {

imageview1= (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);

Imageview1.setimageresource (r.drawable.sample);

edittext1= (EditText) Findviewbyid (R.ID.EDITTEXT1);

Edittext1.addtextchangedlistener (New Textwatcher () {

@Override.

. public void Aftertextchanged (Editable arg0) {

//TODO auto-generated method stub

25.}

@Override

public void beforetextchanged (Charsequence s, int. start, int count,int after) {

A)//TODO auto-generated method stub

28.}

@Override

public void ontextchanged (charsequence s, int start, int before,int count) {

//TODO auto-generated method stub

32.}});

radiogroup1= (Radiogroup) Findviewbyid (r.id.radiogroup1);

Radiogroup1.setoncheckedchangelistener (New

Radiogroup.oncheckedchangelistener ()

{@Override

oncheckedchanged public void (radiogroup arg0, int checkedid)

37. {

. switch (Checkedid)

39. {

Case R.id.radiobutton1:

LOG.D ("Testactivity", "select RadioButton1");

break;

Case R.id.radiobutton2:

LOG.D ("Testactivity", "select RadioButton2");

break;

. Default:

break;

48.}});

checkbox1= (CheckBox) Findviewbyid (r.id.checkbox1);

Checkbox2= (CheckBox) Findviewbyid (R.ID.CHECKBOX2);

Wuyi Oncheckedchangelistener listener = new

Compoundbutton.oncheckedchangelistener ()

52. {

@Override.

The public void oncheckedchanged (Compoundbutton Buttonview, Boolean

isChecked) {

Switch (Buttonview.getid ()) {

Case R.id.checkbox1://action

LOG.D ("Testactivity", "CheckBox1:" +ischecked+ "=" +buttonview.gettext ());

break;

Case R.ID.CHECKBOX2://action

LOG.D ("Testactivity", "CheckBox2:" +ischecked+ "=" +buttonview.gettext ());

break;

Case R.id.togglebutton1://action

LOG.D ("Testactivity", "ToggleButton1:" +ischecked+ "=" +buttonview.gettext ());

"Break";

65.}};

Checkbox1.setoncheckedchangelistener (Lisdtener);

Checkbox2.setoncheckedchangelistener (listener);

togglebutton1= (ToggleButton) Findviewbyid (R.id.togglebutton1);

Togglebutton1.setoncheckedchangelistener (listener);

Datepicker= (DatePicker) Findviewbyid (R.id.datepicker);

Datepicker.init (9, 8, new Datepicker.ondatechangedlistener () {

ondatechanged public void (DatePicker view, int year,int monthofyear, int

DayOfMonth) {

LOG.D ("Testactivity", "DatePicker the date you selected is:" +year+ "Year" + (monthofyear+1) + "month" +
Dayofmonth+ "Day. ");

74.}});

Final string[] from={"China", "United States", "Russia", "Canada"};

spinner1= (Spinner) Findviewbyid (r.id.spinner1);

adapter = newarrayadapter<string> (This,

Android. R.layout.simple_spinner_item, from);

Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);

Spinner1.setadapter (adapter);

Spinner1.setonitemselectedlistener (Newspinner.onitemselectedlistener () {

Bayi. @Override

onitemselected public void (adapterview<?> arg0, View arg1,int arg2,long arg3)

83. {

LOG.D ("testactivity", "I clicked on Spinner1:" +from[arg2]);

85.}

@Override.

onnothingselected public void (adapterview<?> arg0) {

88.}});

button1= (Button) Findviewbyid (R.id.button1);

Button1.setonclicklistener (New View.onclicklistener () {

@Override.

The. public void OnClick (View v) {

LOG.D ("Testactivity:", "I clicked: Button");

94.}});}

95.}

Line 2nd to 10th is the Control object Definition section, which defines the controls used in the main layout file.

Line 12th to 15th in the OnCreate function we set the main layout to the program's view, and then bind the controls and set the listener in the Findwidget function.

The 18th to 19th line is the code of the ImageView control, through the Setimageresource method can be drawable in the picture resources set to the picture.

Line 20th to 32nd is the EditText control, which sets up the Textwatcher listener through Addtextchangedlistener. This listener is called back when the user enters, deletes, and modifies edittext content. Through the beforetextchanged function can get the content before the modification, through the ontextchanged function can know which place is changed, and aftertextchanged function can obtain the modified content. Through these several callback functions can make some judgments, reminders, restrictions on the user input work.

Line 33rd to 48th is a single box consisting of Radiogroup and RadioButton. In the code you only need to get to the Radiogroup object and set the Radiogroup.oncheckedchangelistener listener through Setoncheckedchangelistener. When the user clicks the single option, the system will callback the listener code set, and in the listener's OnCheckedChanged method, the Checkedid will know which RadioButton triggered the Click event and handled it accordingly.

Line 49th to 50th is the CheckBox1 and CheckBox2 objects of two multiple-selection controls.

Line 51st to 65th defines the checked state of the checkbox to change the Listener object Oncheckedchangelistener listener. The system callback function oncheckedchanged is implemented in this object. OnCheckedChanged is called by Buttonview to indicate which control's state has changed and the value to change is ischecked. Here you set the Listener Listener object for CheckBox 1, CheckBox 2, and ToggleButton 1, and then use the Buttonview.getid () function to determine which control state is changing.

Line 66th to 67th sets the listener listener to checkbox 1 and CheckBox 2.

Line 68th to 69th is the ToggleButton control, which also uses the listener listener, which is functionally similar to a checkbox.

Line 70th to 74th is the date selection DatePicker control, the Datepicker.init function is used to initialize the control's month and day, and the date Change Listener Datepicker.ondatechangedlistener () is set. When the user changes the date, the system will call back the listener's ondatechanged function, through which we know the date of the modified month and day data. Note: Monthofyear is counted starting from 0, or 0 for January.

Line 75th to 79th is the spinner list selector and sets the adapter adapter. The string array from inside is the content source of the list, and the content source is connected with the array adapter adapter by setting the Adapter.setdropdownviewresource to the list Simple_spinner_dropdown_ Item display style. Click Spinner after you have shown the 10-7.

▲ Figure 10-7 Spinner popup Selection Box example

The 80th to 88th line sets the spinner Selection Listener Spinner.onitemselectedlistener () through Setonitemselectedlistener, When the user selects the list item, the system callbacks the onitemselected function in the listener, and we are informed by the ARG2 parameter that the first column is selected.

Line 89th to 94th is the normal button, which sets the listener View.onclicklistener through Setonclicklistener. When the user clicks the button, the system callbacks the OnClick method and executes our custom code.

The following is a sample program click on the logcat output graph.

▲ Figure 10-8 Example logcat output diagram

Android Input Control Classic-input controls "pack"

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.