android-Common Parts

Source: Internet
Author: User

1. Create dialog ()

Android offers 4 types of dialog:alertdialog,progressdialog,datepickerdialog,timepickerdialog.

1.0 Alertdialog

1.1 Steps to Call dialog (when the user clicks on an item)

Method of creating dialog using the ShowDialog method (Oncreatedialog method)

@Override Public Booleanonoptionsitemselected (MenuItem item) {//action after selecting options on the option menu             intId=Item.getitemid (); //match the ID of the selected item and call the ShowDialog method Access ID,//Create dialog implemented by the activity's Oncreatedialog method             Switch(ID) { CaseOpton_item_exit:showdialog (opton_item_exit);  Break;  CaseOpton_item_scan:showdialog (Opton_item_scan);  Break; }             return true ; } @Override Public Booleanoncontextitemselected (MenuItem item) {//action after selecting options for context menu            intId=Item.getitemid (); Switch(ID) { CaseContext_option_delete:showdialog (Context_option_delete);  Break;  CaseContext_option_property:showdialog (Context_option_property);  Break; }            return true; }

1.2 Steps to create a dialog:

Overwrite Oncreatedialog method in activity, constant value of item with ID activity

Private Static Final intOpton_item_scan = 101;Private Static Final intContext_option_delete = 100;Private Static Final intContext_option_property = 0; @OverrideprotectedDialog Oncreatedialog (intID) {//match the ID of the incoming item and create the corresponding dialog//use Alertdialog.builder to create dialog for the current context//1. Title: Settitle//2. Two buttons: A negative button: Setnegativebutton, a positive button: Setpositivebutton             Switch(ID) { CaseContext_option_delete:return                          NewAlertdialog.builder ( This). Settitle (R.string.delete_message). Setnegativebutton (R.STRING.BUTTON_OK, 
    NewDialoginterface.onclicklistener () {
     Public voidOnClick (Dialoginterface dialog,intwhich) {                                 //to delete an operation//Delete operations via Contentresolver and ContentProviderContentresolver resolver=Getcontentresolver (); //gets the ID of the selected song                                 intSongid=Cursor.getint (Cursor.getcolumnindexorthrow (mediastore.audio.media._id)); //get the directory of the song's DataString path=cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Audio.Media.DATA)); //gets the record of the URI of the specified ID song from the database,//Contenturis is the action URI class,//it (in its own way: content://authority/path/id) can access a database or a row of recordsUri uri=Contenturis.withappendedid (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, SongID); //Delete records from a song in the databaseResolver.delete (URI,NULL,NULL); //Delete the files in the SD cardFile f=NewFile (path); if(F.exists ()) F.delete (); //notifies the main interface that the song has been deletedToast.maketext (musicdemoactivity. This, "Song has been deleted", 100). Show (); }}). Setpositivebutton (R.string.button_cancel, /c4>NewOnclicklistener () { Public voidOnClick (Dialoginterface dialog,intwhich) {                                     //cancel destroys the dialog, which can be the activity's DismissDialog (show) method//or the dismiss method of the dialog object.DismissDialog (Context_option_delete);                                       }}). Create ();  CaseContext_option_property: Break;  CaseOpton_item_exit: Break;  CaseOpton_item_scan: Break; }                return NULL; }

2. Notify the user

Toast: Prompts the user for a small piece of text information that cannot interact with the user

Notification: Prompts the user in the status bar, the user can interact with Notification, such as text messages come up

Dialog: A small window pops up in front of the active activity to receive user input instead of activity.

3. Handling user Input

Android provides several ways to handle user input, and its core mechanism is to use the Java interface callback (call back) mechanism.

A series of inline listener classes are defined in the View.view class, including:

View.onclicklistener: Monitor Click action

View.onlongclicklistener: Monitor long press action

View.onkeylistener: Monitor Keyboard button action

View.ontouchlistener: Monitor Press, release, or move

View.onfocuschangelistener: Focus enters, or leave view

When View.OnCreateContextMenuListener:contextmenu is created, the call

Oncreatecontextmenu method

4. Styles and themes (style)

Design your own style (styles and themes) for your own interface (TextView widgets, Windows main window).

The styles and themes are created under the Res/values folder with the Android resources XML file,

The root node is <RESOURCES>, the child node <style> is a style/theme, and in the values folder, the writing method is the key-value pair, the key is the property, and the value is the property value.

Style (for a widget such as TextView):

<?XML version= "1.0" encoding= "Utf-8"?><Resources>     <stylename= "Mystyles_textview"         >         <Itemname= "Android:textcolor">#FF0000</Item>     </style></Resources>How to call: In an XML file<TextViewAndroid:id= "@+id/song"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"style= "@style/mystyles_textview"      />

Subject (for Windows):

<?XML version= "1.0" encoding= "Utf-8"?><Resources>    <stylename= "MyTheme"        >        <Itemname= "Android:textcolor">#FF0000</Item>        <Itemname= "Android:windownotitle">True</Item>           <Itemname= "Android:windowbackground">@drawable/sea</Item>    </style></Resources>

Invocation method: In the manifest file

< Application         Android:icon = "@drawable/ic_launcher"         android:label= "@string/app_name"         android:theme= "@style/mytheme"         >

android-Common Parts

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.