[Android, 18] common components in android:

Source: Internet
Author: User
Tags time in milliseconds

1. Use of the notification component:

1. The notification component displays a text message prompt in the phone taskbar when a short message is sent. When you click it, an activity is activated.

 

2. Create and use:

Public void click (Viewview ){

// 1 Get a reference to the icationicationmanager:

// Obtain the administrator of a notification.

Icationicationmanager manager = (NotificationManager) getSystemService (

NOTIFICATION_SERVICE );

 

// 2. initialize the notification to be displayed by the manager.

 

Int icon = R. drawable. ic_launcher;

CharSequence tickerText = "Hello title ";

Long when = System. currentTimeMillis ();

// Constructor parameters: (icon, title, and notification display time)

Icationicationnotification = new Notification (icon, tickerText, when );

 

// 3. Specific notification events

// Specify the sound

Notification. sound = Uri. fromFile (newFile ("/sdcard/hacard "));

Notification. ledARGB // What is the LED light displayed?

// The unit of the specified vibration time in milliseconds

Notification. vibrate (); // parameter long [] array 20 50 80

// Specify the display status of notification. (FLAG_NO_CLEAR: not displayed, FLAG_INSISTENT: always displayed)

Notification. flags = Notification. FLAG_NO_CLEAR;

 

// The intent of the extension

Intent intent = new Intent ();

Intent. setAction (Intent. ACTION_CALL );

Intent. setData (Uri. parse ("tel: 123456 "));

 

// Create extension intent getActivity method parameters (context, 0, intent object, 0)

PendingIntent contentIntent = PendingIntent. getActivity (this, 0, intent, 0 );

Set the extension intent for notification:

Notification. contentIntent = contentIntent;

 

// The setLatestEventInfo () method is to set the content opened by clicking notification and execute the extension intent.

Notification. setLatestEventInfo (this, "title", "body", contentIntent );

// 4. Use the notification manager to display the notification

Manager. Y (0, notification );

 

}

}

Ii. usage of the dialog box:

 

1. Confirm to cancel the dialog box:

Public void click1 (View view ){

// 1. Create a builder in the dialog box

AlertDialog. Builderbuilder = new Builder (this );

Builder. setIcon (R. drawable. ic_launcher );

Builder. setTitle ("I am the title ");

Builder. setMessage ("I am the body ");

// Confirmation button in the dialog box

Builder. setPositiveButton ("OK", newOnClickListener (){

 

@ Override

Public void onClick (DialogInterface dialog, int which ){

// TODO Auto-generatedmethod stub

System. out. println ("OK ");

}

});

// Set the cancel button and click event

Builder. setNegativeButton ("cancel", newOnClickListener (){

 

@ Override

Public void onClick (DialogInterface dialog, int which ){

// TODO Auto-generatedmethod stub

System. out. println ("canceled ");

}

});

// The create dialog box is displayed.

Builder. create (). show ();

}

2. Select dialog box

Public void click2 (View view ){

AlertDialog. Builder builder = new Builder (this );

Final String [] items = {"red", "blue", "green "};

// Set the data and click events in the select dialog box

Builder. setItems (items, new OnClickListener (){

 

Method parameters (currently dialog, the items to be clicked are in the items array subscript)

Public voidonClick (DialogInterface dialog, int which ){

Toast. makeText (getApplicationContext (),

Items [which] + "clicked ",

0). show ();

 

}

});

// Display dialog box

Builder. create (). show ();

}

3. Single-choice dialog box:

Public void click3 (View view ){

Final CharSequence [] items = {"Red", "Green", "Blue "};

 

AlertDialog. Builder builder = new AlertDialog. Builder (this );

Builder. setTitle ("Pick a color ");

// Method parameters (array, the default entry tag-1 is not selected by default, click the event)

Builder. setSingleChoiceItems (items,-1,

New DialogInterface. OnClickListener (){

Public voidonClick (DialogInterface dialog, int item ){

Toast. makeText (getApplicationContext (), items [item],

Toast. LENGTH_SHORT). show ();

// Close the dialog box

Dialog. dismiss ();

}

});

AlertDialog alert = builder. create ();

Alert. show ();

}

4. Multiple choice dialog box:

Public void click4 (View view ){

Final CharSequence [] items = {"Red", "Green", "Blue "};

 

AlertDialog. Builder builder = new AlertDialog. Builder (this );

Builder. setTitle ("Pick a color ");

// Method parameters: (array, default selected array. "true" indicates that the corresponding entry is selected. Click the event)

Builder. setMultiChoiceItems (items,

New boolean [] {false, true, false },

New OnMultiChoiceClickListener (){

 

@ Override

Public voidonClick (DialogInterface dialog, int which,

Boolean isChecked ){

 

Toast. makeText (getApplicationContext (),

Items [which] + "" + isChecked,

Toast. LENGTH_SHORT). show ();

}

});

// Set the confirmation button

Builder. setPositiveButton ("OK", newOnClickListener (){

 

@ Override

Public voidonClick (DialogInterface dialog, int which ){

// TODO Auto-generated method stub

 

}

});

 

AlertDialog alert = builder. create ();

Alert. show ();

}

 

5. progress bar dialog box: ProgressDialog class

Public void click5 (Viewview ){

ProgressDialog pd = new ProgressDialog (this );

Pd. setTitle ("prompt title ");

Pd. setMessage ("processing ...");

Pd. show ();

}

 

Iii. drop-down list Components

1. Components in the drop-down list:

Android: id = "@ + id/spinner"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"/>

 

2. Operations in the drop-down list:

// Retrieve the drop-down list component

Spinner spinner = (Spinner) this. findViewById (R. id. spinner );

// Create an adapter

ArrayAdapter Adapter = newArrayAdapter (This,

Android. R. layout. simple_spinner_item );

// Set the layout for adaptation

// Android. R. layout. simple_spinner_dropdown_item

Adapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );

Adapter. add ("java ");

Adapter. add ("dotNet ");

Adapter. add ("php ");

 

Spinner. setAdapter (adapter );

// Click an event

Spinner. setOnItemSelectedListener (new OnItemSelectedListener (){

 

@ Override

Public voidonItemSelected (AdapterView Parent, View view,

Int position, long id ){

// TODO Auto-generated method stub

 

}

 

@ Override

Public voidonNothingSelected (AdapterView Parent ){

// TODO Auto-generated method stub

 

}

});

 

4. Dragging:

1. Drag a widget:

Android: id = "@ + id/seekBar1"

Android: layout_width = "match_parent"

Android: layout_height = "wrap_content"/>

2. component usage:

// Drag the Component Object

SeekBar1 = (SeekBar) this. findViewById (R. id. seekBar1 );

 

SeekBar1.setMax (100); // you can specify the maximum size of a dragged bar.

SeekBar1.setProgress (50); // drag the current value of the bar

// Drag event

SeekBar1.setOnSeekBarChangeListener (new OnSeekBarChangeListener (){

 

// Method for stopping a drag

Public voidonStopTrackingTouch (SeekBar seekBar ){

// TODO Auto-generated method stub

System. out. println ("stopped dragging ");

// Obtain the current location

Int position = seekBar. getProgress ();

 

}

 

@ Override

// Method to start dragging:

Public voidonStartTrackingTouch (SeekBar seekBar ){

// TODO Auto-generated method stub

System. out. println ("started dragging ");

 

}

 

@ Override

Public voidonProgressChanged (SeekBar seekBar, int progress,

Boolean fromUser ){

// TODO Auto-generated method stub

 

}

});

 

5. Menu:

Override two methods in the activity class:

 

@ Override // set menu items

Public boolean onCreateOptionsMenu (Menu menu ){

Menu. add (Menu. NONE, 1, Menu. NONE, "add ");

Menu. add (Menu. NONE, 2, Menu. NONE, "Update ");

Return super. onCreateOptionsMenu (menu );

}

 

@ Override // click the menu item event

Public boolean onMenuItemSelected (int featureId, MenuItem item ){

Switch (item. getItemId ()){

Case 1: // Add

Log. I ("tag", "add was selected ");

Return true;

Case 2: // update

Log. I ("tag", "update was selected ");

Return true;

Default:

Returnsuper. onMenuItemSelected (featureId, item );

}

}

6. Enter the content to automatically complete the text box (AutoCompleteTextView)

1. Components

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: completionThreshold = "1"

Android: id = "@ + id/name"/>

Bytes

2. Operation:

Public void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. main );

String [] names = {"Lao Zhang", "Lao Fang", "Lao Bi", "Li Ming", "Li", "Chen Jiang", "abc", "acc "};

AutoCompleteTextViewnameText = (AutoCompleteTextView)

This. findViewById (R. id. name );

ArrayAdapter Adapter = new ArrayAdapter (

This, android. R. layout. simple_dropdown_item_1line, names );

NameText. setAdapter (adapter );

}

 

VII. ScrollView: scrolling content

 

 

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: orientation = "vertical">

 

Android: layout_width = "match_parent"

Android: layout_height = "match_parent"

Android: id = "@ + id/TV"

/>

 

 

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.