Android learning notes-basic introduction to Widgets

Source: Internet
Author: User

 

I simply learned about the widget components of Android. And their implementation methods.

First, let's talk about the most commonly used button.

1. Button button

Create a button in the main. xml file

Public class mainactivity extends activity {
 
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button button = (button) findviewbyid (R. Id. Button );
Button. setonclicklistener (button_listener );
}

First bind R. layout. Main. Then use the findviewbyid method to create a button and use the setonclicklistener method to listen to it. The parameter is botton_listener.

This parameter is defined in this way.

Private button. onclicklistener button_listener = new button. onclicklistener (){
Public void onclick (view v ){
Settitle ("click ");
}
};

An anonymous internal class is used to create an object for the onclicklistener interface and implement it in detail. Set the title of the current activity to "click ".

2. textview label (text box that cannot be edited)

This is its specific property

<Textview
Android: Id = "@ + ID/text_view"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textsize = "16sp" proportional Pixel
Android: textcolor = "# ffffff"
Android: padding = "10dip"
Android: Background = "# cc0000"
Android: text = "textview, where you can enter the text to be displayed .."
/>

3. edittext text box (editable)

Same as above

4. checkbox multiple selection box

<Checkbox Android: Id = "@ + ID/plain_cb"
Android: text = "plain"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
 
<Checkbox Android: Id = "@ + ID/serif_cb"
Android: text = "serif"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Typeface = "serif"
/>
 
<Checkbox Android: Id = "@ + ID/bold_cb"
Android: text = "bold"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textstyle = "bold"
/>
Each checkbox has its own ID, its own text, and its own style. This is basically the same as HTML.

5. radiogroup single choice

<Radiogroup
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Orientation = "vertical"
Android: checkedbutton = "@ + ID/lunch"
Android: Id = "@ + ID/menu">
<Radiobutton
Android: text = "Breakfast"
Android: Id = "@ + ID/breakfast"
/>
<Radiobutton
Android: text = "lunch"
Android: Id = "@ ID/lunch"/>
<Radiobutton
Android: text = "dinner"
Android: Id = "@ + ID/dinner"/>
<Radiobutton
Android: text = "all"
Android: Id = "@ + ID/all"/>
</Radiogroup>

A radiogroup component has its own ID and contains multiple radiobutton components. Each of them has its own ID. The selected event is also ischecked ().

6. spinner drop-down list

The drop-down list must contain content. It seems that the implementation method of this component is relatively troublesome. There are two implementation methods.

(1) first define several strings to exist in the array

Private Static final string [] mcountries = {"China", "Russia", "Germany ",
"Ukraine", "Belarus", "USA "};

Spinner_c = (spinner) findviewbyid (R. Id. spinner_1 );
Allcountries = new arraylist <string> (); defines an arraylist <string>
For (INT I = 0; I <mcountries. length; I ++ ){
Allcountries. Add (mcountries [I]); traverse the array to store the content in the Set
}
Aspncountries = new arrayadapter <string> (this, put the set in the arrayadapter pair
Android. R. layout. simple_spinner_item, allcountries );
Aspncountries
. Setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
Spinner_c.setadapter (aspncountries );

Call the setdropdownviewresource method of the arrayadapter object

Finally, use the setadapter method of the spinner object to add the content.

(2) You can also pre-define data in an XML file.

Spinner_2 = (spinner) findviewbyid (R. Id. spinner_2 );
Arrayadapter <charsequence> adapter = arrayadapter. createfromresource (
This, R. array. Countries, Android. R. layout. simple_spinner_item );
Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );
Spinner_2.setadapter (adapter );

<Resources>
<! -- Used in spinner/spinner_2.java -->
<String-array name = "countries">
<Item> china2 </item>
<Item> russia2 </item>
<Item> germany2 </item>
<Item> ukraine2 </item>
<Item> belarus2 </item>
<Item> usa2 </item>
</String-array>
</Resources>

Create an array. xml file in the Res/values/directory and find it through R. array. Countries

7. autocompletetextview automatically completes text

I think this component is awesome. Currently, only one simple automatic matching function is available.

Arrayadapter <string> adapter = new arrayadapter <string> (this,
Android. R. layout. simple_dropdown_item_1line, countries );
Autocompletetextview textview = (autocompletetextview) findviewbyid (R. Id. auto_complete );
Textview. setadapter (adapter );

Static final string [] countries = new string [] {
"China", "Russia", "Germany ",
"Ukraine", "Belarus", "USA", "china1", "china12", "Germany ",
"Russia2", "Belarus", "USA"
};

Bind the content of the countries Array

Corresponding XML

<Autocompletetextview Android: Id = "@ + ID/auto_complete"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

The matching content in the input ch is automatically displayed. This is very powerful, and it is not deeply understood.

8. datepicker date Selector

Datepicker dp = (datepicker) This. findviewbyid (R. Id. date_picker );
DP. INIT (2009, 5, 17, null );

Initialize using init

<Datepicker
Android: Id = "@ + ID/date_picker"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
9. timepicker

Similar to the above datepicker

10. scrollview

When the content displayed on the main interface exceeds one screen, you can add

<Scrollview xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">

Use this component to pack it, so that it is OK.

11. progress bar

<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "circular progress bar"/>
<Progressbar
Android: Id = "@ + ID/progress_bar"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>

<Textview
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Horizontal progress bar"/>
<Progressbar Android: Id = "@ + ID/progress_horizontal"
Style = "? Android: ATTR/progressbarstylehorizontal"
Android: layout_width = "200dip"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: Progress = "50"
Android: secondaryprogress = "75"/>
</Linearlayout>

12. seekbar

<Seekbar
Android: Id = "@ + ID/seek"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: max = "100"
Android: thumb = "@ drawable/Seeker"
Android: Progress = "50"/>

Android: thumb = "@ drawable/Seeker": You can set the image of the drag chart.

13. imageview Image view

<Imageview
Android: Id = "@ + ID/imagebutton"
Android: src = "@ drawable/EOE"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>

It is very easy to display a specified image.

14. imageswitcher & Gallery (Gallery) switch between images

This component is quite complex.

<Relativelayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<Imageswitcher
Android: Id = "@ + ID/switcher"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_alignparenttop = "true"
Android: layout_alignparentleft = "true"/>

<Gallery Android: Id = "@ + ID/Gallery"
Android: Background = "#55000000"
Android: layout_width = "fill_parent"
Android: layout_height = "60dp"
Android: layout_alignparentbottom = "true"
Android: layout_alignparentleft = "true"
Android: gravity = "center_vertical"
Android: spacing = "16dp"/>
</Relativelayout>

This is its XML file. It is divided into two parts: imageswitcher and gallery. The final implementation is to display a large image on the screen, and a group of scrolling images under the screen.

The code is complicated. I will not introduce it here.

15. tabhost switch panel

Tabhost = gettabhost ();
Layoutinflater. From (this). Inflate (R. layout. tab_demo,
Tabhost. gettabcontentview (), true );
Tabhost. addtab (tabhost. newtabspec ("tab1"). setindicator ("tab1 ")
. Setcontent (R. Id. view1 ));
Tabhost. addtab (tabhost. newtabspec ("tab3"). setindicator ("tab2 ")
. Setcontent (R. Id. view2 ));
Tabhost. addtab (tabhost. newtabspec ("tab3"). setindicator ("tab3 ")
. Setcontent (R. Id. view3 ));

First, gettabhost () gets a tabhost, then binds the template, binds the content of each tag, and looks at the template file.

<Framelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">

<Textview Android: Id = "@ + ID/view1"
Android: Background = "@ drawable/blue"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "here is the content in tab1. "/>

<Textview Android: Id = "@ + ID/view2"
Android: Background = "@ drawable/red"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "here is tab2, balabalal ..... "/>

<Textview Android: Id = "@ + ID/view3"
Android: Background = "@ drawable/Green"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: text = "tab3"/>

</Framelayout>
There are basically so many components. I feel that I have to learn more and try more. In order to learn well.

 

Original article: http://blog.sina.com.cn/s/blog_61c62a960100ev5k.html

 

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.