Android Interface Programming Adventure (II)

Source: Internet
Author: User
Tags event listener

This article will start the Android Interface programming Adventure (ii) journey.


1.UI Component-adapterview and its subclasses (ii)


1) Use of Auto-complete text box (Autocompletetextview)

Multiautocompletetextview is a autocompletetextview derived subclass, Multiautocompletetextview allows multiple prompt entries to be entered, and multiple prompt entries separated by delimiters. Here's a brief demonstration of the use of Autocompletetextview and Multiautocompletetextview:

Main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><!--define an auto-complete text box, specifying that a character is entered to prompt--><autocompletetextview  android:id= "@+id/auto" Android:layout_ Width= "Fill_parent" android:layout_height= "wrap_content" android:completionhint= "Please select:" Android: dropdownhorizontaloffset= "10DP" android:completionthreshold= "1"/><!--define a Multiautocompletetextview component-- ><multiautocompletetextviewandroid:id= "@+id/mauto" android:layout_width= "Fill_parent" Android:layout_ height= "Wrap_content" android:completionthreshold= "1"/></linearlayout>
Autocompletetextviewtest.java
public class Autocompletetextviewtest extends Activity {Autocompletetextview auto; Multiautocompletetextview Mauto; string[] str = new string[]{"AA", "AB", "AC",}; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.main);//Create Arrayadapter object arrayadapter<string> adapter = new Arrayadapter<string> (this, Android. R.layout.simple_dropdown_item_1line, str); auto = (Autocompletetextview) Findviewbyid (R.id.auto); Auto.setAdapter ( adapter); Mauto = (Multiautocompletetextview) Findviewbyid (R.id.mauto); Mauto.setadapter (adapter);// Set the delimiter Mauto.settokenizer (New Multiautocompletetextview.commatokenizer ()) for Multiautocompletetextview;}}

The effect of running as above code is as follows:


2) Usage of the grid view (GridView)

The only difference between the GridView and the ListView is that the ListView displays only one column, and the GridView can display multiple columns. The XML attributes commonly used by the GridView are as follows:

Android:columnwidth= "" Set column width android:numcolumns= "" Set Number of columns android:gravity= "" Set alignment android:verticalspacing= "" Set the vertical distance between elements android:horizontalspacing= "" sets the horizontal distance between elements android:stretchmode= "" Sets the stretch mode
the adapter of the GridView is referenced in the ListView. 3) Other

This section of the component only gives a demonstration effect, the use of the method can be consulted official documents.

Expandable List Component (Expandablelistview)     

Pop-up menu for User selection (Spinner)

Gallery View (Gallery)


use Adapterviewflipper to automatically play pictures                 

StackView implementation to display multiple list items in a "stacked" manner




2.UI Component-progressbar and its subclasses


1) Usage of ProgressBar

ProgressBar represents a progress bar component, which is typically used to show a time-consuming completion percentage, and the ProgressBar and its subclass class diagram are as follows:


The style of ProgressBar can be set by the Style property, which has several property values:

<!--define a large circular progress bar--><progressbar android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Style= "@android: Style/widget.progressbar.large"/><!--define a medium-sized ring progress bar--><progressbar Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content"/>style= "@android: Style/widget.progressbar.small" Define a small loop progress bar style= "@android: Style/widget.progressbar.horizontal" defines a horizontal progress bar style= "@android: style/ Widget.ProgressBar.Horizontal "defines a horizontal progress bar and changes the appearance of the track
the XML attributes commonly used by ProgressBar are as follows:
android:max= "100" set the maximum value of the progress bar android:progress= "" Set progress bar completed progress value android:progressdrawable= "@drawable/..." Set the track for the progress bar to the Drawable object android:indeterminate= "" True to indicate that the progress bar is not accurately displaying progress android:indeterminatedrawable= "" Set the Drawable object to draw a progress bar that does not show progress android:indeterminateduration= "" setting does not accurately show the duration of progress
2) Usage of the drag bar (SeekBar)

Seekbar application is relatively small (typical applications have drag to adjust the volume, etc.), Seekbar use please consult the official documentation.

3) Usage of star rating Bar (Ratingbar)

Ratingbar and Seekbar are very similar, they are the biggest difference is ratingbar through the stars to express progress, Ratingbar usage please consult official documents.



3.UI Component-viewanimator and its subclasses


the main function of Viewanimator and its subclasses is to increase the animation effect, the class diagram is as follows:


Viewswitcher: View switch component, can realize the effect of split screen, left and right scrolling.

Imageswitcher: An image switcher that lets you use animation effects when switching view components.

Textswitcher: Text switcher, which can be animated when switching view components, differs from Imageswitcher Textswitcher required Viewfactory Makeview () method must return a TextView component Textswitcher and TextView function Similarly, can display text content, the difference is that textswitcher can specify the animation effect when the text is toggled.



4. dialog box


Android offers 4 common dialog boxes:

Alertdialog: The most versatile and most widely used dialog box

ProgressDialog: progress dialog box

Datepickerdialog: Date selection dialog box

Timepickerdialog: Time Selection dialog box

1) Create a dialog box with Alertdialog

Alertdialog provides the following 6 ways to specify the contents of a dialog box:

Setmessage () Settings dialog box contents for simple text content Setitems () Setting dialog box contents for simple list items Setsinglechoiceitems () Setting dialog box contents for single-selection list items Setmultichoiceitems () The Settings dialog box content is a multi-select list item setadapter () Setting dialog box content for a custom list item Setview () Setting dialog box content for a custom view
Here's a look at the specific usage of Alertdialog:1-1) dialog box showing the prompt message
/** * Simple dialog box Click event * @param view */public void Simpledialog (view view) {New Alertdialog.builder (this). Settitle ("Simple Dialog")//settings dialog box caption. SetIcon (R.drawable.tools)//Set icon. Setcancelable (FALSE)//set Click on the outside of the dialog interface does not disappear, press the return key does not work//. Setcanceledontouchoutside (FALSE); Setting the screen outside of dialog does not disappear, pressing the back button also works. Setmessage ("OK to exit?" "). Setpositivebutton (" OK ", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) { Toast.maketext (Getapplicationcontext (), "clicked the OK button!" ", 0). Show ();}}). Setnegativebutton ("Cancel", null). Create (). Show ();
:



2) Create a dialog box with ProgressDialog2-1) Loop progress bar
public void Showspinner (view view) {//Call static method to display the Ring progress bar progressdialog.show (this, "Task execution," "Task execution, wait", false, True);}
:

2-2) Progress bar showing progress
public class Progressdialogtest extends Activity {final static int max_progress = 100;private int[] data = new INT[50];// The program simulates an array with a padding length of 100 int progressstatus = 0; The percent complete of the Record Progress dialog box int hasData = 0;  ProgressDialog pd;/** * Defines a Handler that is responsible for the progress of the update */handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {//indicates that the message is sent by the program if (Msg.what = = 0) {pd.setprogress (progressstatus);}}; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main);}  /** * Show progress bar Click event * @param source */public void ShowProgress (View source) {progressstatus = 0;//reset progress bar completion progress to 0hasData = 0; Restart populating array pd = new ProgressDialog (progressdialogtest.this);pd. Setmax (max_progress);pd. Settitle ("percent of task completion"); Set the title of the dialog box Pd.setmessage ("percent of time-consuming task completion"); The Settings dialog box displays the content pd.setcancelable (false); The settings dialog cannot be closed with the "Cancel" button Pd.setprogressstyle (progressdialog.style_horizontal); Sets the progress bar style pd.setindeterminate (false) for the dialog box; Sets whether the progress bar of the dialog box shows Progress Pd.show (); New Thread () {public voidRun () {while (Progressstatus < max_progress) {progressstatus = max_progress * DoWork ()/data.length;//get% complete for time-consuming operations ha Ndler.sendemptymessage (0); Send an empty message to handler}//if the task has completed if (progressstatus >= max_progress) {//Close the dialog box Pd.dismiss ();}}. Start ();} /** * Simulates a time-consuming operation * @return */public int doWork () {data[hasdata++] = (int) (Math.random () * 100);//array element assignment try {Thread.slee P (100);} catch (Interruptedexception e) {e.printstacktrace ();} return hasData;}}
:




5. Menu


The menu support class diagram for the Android system is as follows:


ContextMenu: Context Menu

Submenu: Sub-menu

MenuItem: Menu item

1) Create an imitation pop-up menu using PopupMenu

Popupmenutest.java

public class Popupmenutest extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main);} PopupMenu popup = null;/** * Pop-up menu click event * @param button */public void Onpopupbuttonclick (View button) {popup = new POPUPME Nu (this, button); Create a PopupMenu object//Load the R.menu.popup_menu menu resource into the popup menu Getmenuinflater (). Inflate (R.menu.popup_menu, Popup.getmenu ()) ;//menu item for the popup menu click event Binding Event Listener Popup.setonmenuitemclicklistener (new Popupmenu.onmenuitemclicklistener () {@ Overridepublic boolean Onmenuitemclick (MenuItem item) {switch (Item.getitemid ()) {case r.id.item1:// Hide the dialog box Popup.dismiss (); break;default://use Toast to display the menu item that the user clicked Toast.maketext (popupmenutest.this, "you clicked" + Item.gettitle ( ) + "menu item", 0). Show (); return true;}}); Popup.show ();}}
where Res/menu/popup_menu.xml is
<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item        android:id= "@+id/item1"        android:icon= "@android:d rawable/ic_menu_search"        android: Textsize= "16SP"        android:title= "initiates group chat"/>    <item        android:id= "@+id/item2"        android:icon= "@ Android:drawable/ic_menu_add "        android:textsize=" 16sp "        android:title=" add Friend "/>    <item        Android:id= "@+id/item3"        android:textsize= "16sp"        android:title= "sweep"/>    <item        android: Id= "@+id/item4"        android:textsize= "16sp"        android:title= "collect payment"/></menu>

Styles.xml as follows

<resources>    <!--application Theme--    <style name= "Apptheme" parent= "Android: Theme.Holo.Light.DarkActionBar "></style></resources>

Run as follows:


Android Interface Programming Adventure (II)

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.