Android Learning 1 ---- learning controls, android1 ----

Source: Internet
Author: User

Android Learning 1 ---- learning controls, android1 ----

User-machine interfaces can be divided into views, view containers, and layout. A complex Android Interface Design usually requires a combination of different components for implementation. the features and functions of the main Android components are not described here.

1. TextView Control

Format of the TextView control:

<TextView

Android: attribute 1 = "attribute value 1"

Android: Property 2 = "property value 2"/>

Properties of the TextView Control

(1) android: id = "@ + id/id of the current control" ----- id of the Current Control

(2) android: layout_width = "property value" ----- indicates the width of the Current Control

(3) android: layout_height = "property value" ----- indicates the height of the Current Control

(2) (3) Each has three attribute values: fill_parent, match_parent, and wrap_content;

Fill_parent: Specifies the width or height of the screen.

Match_parent: indicates that the height or width is the same as that of the parent element.

Wrap_content: indicates that the width or height of the control changes with the size of the control content.

(4) android: text = "@ string/name" ----- indicates the content to be displayed in the current TextView Control

(5) android: textSize = "property value" ----- indicates the size of the text content of the current control

(6) android: textColor = "attribute value (generally RGB color # *******)" ----- indicates the text color of the Current Control

Other attributes will be learned later, and the article will be supplemented and improved.

2. EditText Control

For the format of the EditText control, see TextView.

The attributes of the EditText control include all the attributes listed in the TextView, as well as common special attributes.

(1) android: hint = "property value" ----- indicates the prompt character in the current input box. when you enter a new character, it is automatically deleted.

 

3. ImageView Control

ImageView format (same as above, which is not described below)

ImageView attributes (including most of the preceding attributes, which are listed below, the same below)

(1) android: src = "attribute value (it is recommended to set the attribute value in the form of @ drawable/name)" ----- indicates the image to be displayed

(2) android: background = "property value (which can be an image or color)" ----- indicates the background image or background color of the Current Control

4. Button and ImageButton controls

Attributes not available for ImageButton

(1) android: text = "property value" ----- used to display the text on the current button

It also has attributes common to ImageButton.

(2) android: backgroundColor = "color value" ----- indicates the background color of the current button

The ImageButton control has unique properties.

(3) android: src = "property value" ----- indicates the image on the current control, because the control itself is an image button.

When using the listener, you need to set the listener OnClickListener in the Activity.

Procedure:

Step 1: Initialize the control

Example: Button bt = (Button) findViewById (R. id. button1); ---- findViewById returns a View class, which needs to be forcibly converted to Button (the same below)

Step 2: Configure listeners (three methods are introduced later ))

Example (anonymous internal class): bt. setOnClickListener (new OnClickListener (){

Public void onClick (){

// TODO

}

});

Step 3: implement the operation in the listener (step 2 to implement)

5. AutoCompleteTextView and MultiAutoCompleteTextview

Common feature: both controls enable automatic match of input text.

Difference: AutoCompleteTextView matches a single text, that is, only one content can be entered in the text box.

MultiAutoCompleteTextView matches multiple input values, that is, when you input a string that can be matched,

Separator, and then you can continue to enter the next string, and can also match.

Attributes to be set

(1) android: completionThreshold = "attribute value (int type)" ----- indicates that the matching starts after "attribute value" is entered.

You need to set the adapter when using it.

Procedure:

Step 1: Initialize the control (same as above)

Step 2: An adapter is required. Generally, ArrayAdapter is used simply.

Step 3: Initialize the data resource, that is, set an array to pre-store some strings to match the input strings

Step 4: bind the current control to the adapter

Step 5: Set Separators

Example:

AcTextView = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView1 );
ArrayAdapter <String> adapter =/* Step 2 */new ArrayAdapter <String> (this, android. R. layout. simple_list_item_1,/* Step 3 */res );
/* Step 4 */
AcTextView. setAdapter (adapter );

/* Step 5 (only required for MultiAutoCompleteTextView) */macTextView. setTokenizer (new MultiAutoCompleteTextView. CommaTokenizer ());

6. ToggleButton

Use the same format as above

Attribute (including the above generic attributes)

(1) android: checked = "true or false" ----- indicates whether the current control is on or off. The current control has two statuses, which are different from other buttons.

(2) android: textOn = "attribute value (generally written as" open ")" ----- indicates the text displayed when the current control is on (when checked = "true ")

(3) android: textOff = "property value (usually" off ")" ----- indicates the text displayed when the current control is off (when checked = "false ")

Listeners are also required for use.

7. CheckBox

Use the same format as above

Attribute (including the above generic attributes)

(1) android: checked = "" ----- meaning the same as above, but only one of the selected or not

(2) android: text = "" ----- indicates the value displayed after the current control

Use the OnCheckedChangeListener listener

 

8. RadioGroup and RadioButton

Similar to CheckBox

The difference is that Radio is a single listener and also uses the OnCheckedChangeListener listener.

Generally, RadioButton is not used separately. Instead, RadioButton is put into the RadioGroup and used as a group of buttons.

 

The preceding 1, 2, 3, 4 DEMO code contains activity code and xml Code activity code package com. example. helloworldtext; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity implements OnClickListener {public Button loginButton; private Button button2, butt On3, button4; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/** 1. initialize the currently required control * use the findViewById () method to initialize the View object returned by the * findViewById () method of the current control. You need to convert it down to the Button object ** 2. set the listener */loginButton = (Button) findViewById (R. id. button1); loginButton. setOnClickListener (new OnClickListener () {/** Method 1: anonymous internal class */@ Overridep Ublic void onClick (View arg0) {// TODO Auto-generated method stubSystem. out. println ("My Button has been clicked") ;}}); button2 = (Button) findViewById (R. id. button2); button3 = (Button) findViewById (R. id. button3); button4 = (Button) findViewById (R. id. button4); button2.setOnClickListener (new MyOnClickListener () {@ Override public void onClick (View arg0) {super. onClick (arg0); Toast. makeText (MainActivity. this, "button2 The logic to be executed ", 1 ). show () ;}}); button3.setOnClickListener (new MyOnClickListener () {@ Override public void onClick (View arg0) {super. onClick (arg0); Toast. makeText (MainActivity. this, "button3 logic to be executed", 1 ). show () ;}}); button4.setOnClickListener (this) ;}@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubLog. I ("tag", "method 4");} class MyOnClickListener implements OnClickListener {@ Ove Rridepublic void onClick (View arg0) {// TODO Auto-generated method stubLog. I ("tag", "parent class onclick event");} xml Code <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/activity_horiz Ontal_margin "android: paddingRight =" @ dimen/activity_horizontal_margin "android: paddingTop =" @ dimen/activity_vertical_margin "tools: context = ". mainActivity "> <TextView android: id =" @ + id/textView1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentLeft =" true "android: layout_alignParentTop = "true" android: text = "@ string/yourName" android: textColor = "#00ff00 "Android: textSize =" 28sp "/> <EditText android: id =" @ + id/editText1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignTop = "@ + id/textView1" android: layout_toRightOf = "@ + id/textView1" android: EMS = "10" android: hint = "@ string/hintName" android: inputType = "textPersonName" android: textSize = "24sp"> <requestFocus/> </EditText> <Button android: id = "@ + id/button1" android Oid: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_centerHorizontal = "true" android: layout_centerVertical = "true" android: text = "@ string/buttunName" android: background = "# ff3300"/> <Button android: id = "@ + id/button3" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/button1" android: layout_alignParentBottom = "true" and Roid: text = "Button 3"/> <Button android: id = "@ + id/button2" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_above = "@ + id/button3" android: layout_alignLeft = "@ + id/button3" android: text = "Button 2"/> <Button android: id = "@ + id/button4" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_below = "@ + id/button1" android: layout_centerHorizontal =" True "android: layout_marginTop =" 14dp "android: text =" button 4 "/> <ImageView android: id =" @ + id/imageView1 "android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_alignRight = "@ + id/editText1" android: layout_below = "@ + id/editText1" android: background = "@ drawable/ic_launcher"/> <! -- Achieve the text drive effect android: ellipsize = "marquee" android: focusable = "true" android: focusableInTouchMode = "true" android: singleLine = "true" --> <com. example. helloworldtext. marQueeTextView android: id = "@ + id/textView2" android: ellipsize = "marquee" android: focusable = "true" android: focusableInTouchMode = "true" android: singleLine = "true" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: Layout_below = "@ + id/imageView1" android: layout_centerHorizontal = "true" android: layout_marginTop = "18dp" android: text = "I Am a TextView and I am a TextView, I am a TextView "/> <com. example. helloworldtext. marQueeTextView android: id = "@ + id/textView3" android: ellipsize = "marquee" android: focusable = "true" android: focusableInTouchMode = "true" android: singleLine = "true" android: layout_width = "wrap_content" android: layout_hei Ght = "wrap_content" android: layout_alignLeft = "@ + id/textView2" android: layout_below = "@ + id/textView2" android: layout_marginTop = "16dp" android: text = "I Am a TextView, I am a TextView, I am a TextView"/> </RelativeLayout> the Demo code of the above 5, contains activity code and xml Code activity code package com. example. demon1; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. widget. arrayAdapter; import android. widg Et. autoCompleteTextView; import android. widget. extends; public class MainActivity extends Activity {private AutoCompleteTextView acTextView; private MultiAutoCompleteTextView macTextView; private String [] res = {"android1", "android2", "anroid3", "beijing1 ", "beijing2", "beijing3", "shanghai1", "shanghai2", "shnghai3"};/** Step 1: Initialize the control * Step 2: Construct the adapter * Step 3: initialize the data source-used to match the current input * Step 4: bind the adapter with the current control Set */@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/* Step 1 */acTextView = (AutoCompleteTextView) findViewById (R. id. autoCompleteTextView1); ArrayAdapter <String> adapter =/* Step 2 */new ArrayAdapter <String> (this, android. r. layout. simple_list_item_1,/* Step 3 */res);/* Step 4 */acTextView. setAdapter (adapter);/** Step 1: Initialize Control * Step 2: Need an adapter * Step 3: Initialize the data source to match the input data * Step 4: bind the current control to the adapter * Step 5: set the separator */macTextView = (MultiAutoCompleteTextView) findViewById (R. id. multiAutoCompleteTextView1); macTextView. setAdapter (adapter); macTextView. setTokenizer (new MultiAutoCompleteTextView. commaTokenizer ();} xml Code <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_hei Ght = "fill_parent" android: orientation = "vertical"> <AutoCompleteTextView android: id = "@ + id/autoCompleteTextView1" android: completionThreshold = "3" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "Enter the query string"> <requestFocus/> </AutoCompleteTextView> <MultiAutoCompleteTextView android: id = "@ + id/multiAutoCompleteTextView1" android: completionThreshold = "2" android: la Yout_width = "match_parent" android: layout_height = "wrap_content" android: hint = "input recipient"/> </LinearLayout> the Demo code for the preceding six steps, contains activity code and xml Code activity code package com. example. demo2; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. imageButton; import android. widget. imag EView; import android. widget. toggleButton; public class MainActivity extends Activity implements OnCheckedChangeListener {private ToggleButton tb; private ImageView img; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main);/** 1. initialize the control * 2. set listener */tb = (ToggleButton) findViewById (R. id. toggleButton1); img = (ImageView) FindViewById (R. id. imageView1); tb. setOnCheckedChangeListener (this) ;}@ Overridepublic void onCheckedChanged (CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubimg. setBackgroundResource (arg1? R. drawable. on: R. drawable. off);} xml Code <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <ToggleButton android: id = "@ + id/toggleButton1" android: textOff = "off" android: textOn = "on" android: layout_width = "match_parent" android: layout_height = "wrap_content"/> <ImageView android: id = "@ + id/imageView1" android: layout_width = "match_parent" android: layout_height = "match_parent"/> </LinearLayout> the preceding 7 or 8 DEMO code contains the activity code and xml Code activity code package com. example. demon3; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. menu; import android. widget. checkBox; import android. widget. checkable; import android. widget. compoundButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. radioButton; import android. widget. radioGroup; public class MainActivity extends Activity implements android. widget. radioGroup. onCheckedChangeListener {private CheckBox check; private RadioGroup rg; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); check = (CheckBox) findViewById (R. id. checkBox1); check. setOnCheckedChangeListener (new OnCheckedChangeListener () {@ Overridepublic void onCheckedChanged (CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubif (arg1) {Log. I ("tag", "married") ;}}); rg = (RadioGroup) findViewById (R. id. radioGroup1); rg. setOnCheckedChangeListener (this) ;}@ Overridepublic void onCheckedChanged (RadioGroup arg0, int arg1) {// TODO Auto-generated method stubswitch (arg1) {case R. id. radio0: Log. I ("tag", "male"); break; case R. id. radio1: Log. I ("tag", "female"); break ;}} xml Code <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <CheckBox android: id = "@ + id/checkBox1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: checked = "false" android: text = "married"/> <RadioGroup android: id = "@ + id/radioGroup1" android: orientation = "horizontal" android: layout_width = "wrap_content" android: layout_height = "wrap_content"> <RadioButton android: id = "@ + id/radio0" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: checked = "true" android: text = "male"/> <RadioButton android: id = "@ + id/radio1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </RadioGroup> </LinearLayout>

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.