In this article, we will mainly talk about the knowledge points mentioned in the title, that is, the component development for Android UI development. I will explain and give examples one by one, hope to help you.
1. Dialog box)
1. Brief Introduction:
A dialog box is an interface element displayed on the Activity. Common dialogs include the following:
1) AlertDialog prompt dialog box)
2) ProgressDialog progress dialog box)
3) DatePickerDialog date selection dialog box)
4) TimePickerDialog Time Selection dialog box)
2. AlertDialog
1) MainActivity. java
Package com. example. rochelle 0826_dialog; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {private View view; @ Override protected void onCreate (Bun Dle savedInstanceState) {super. onCreate (savedInstanceState); LayoutInflater lf = (LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE); view = lf. inflate (R. layout. cell, null); // obtain the AlertDialog static internal class Builder object and create the AlertDialog dialog box. builder AB = new AlertDialog. builder (this); AB. setView (view); // set the title of the dialog box AB. setTitle ("Tip:"); // set the content of the dialog box AB. setMessage ("are you sure you want to exit the application? "); // Set the icon AB in the dialog box. setIcon (R. drawable. ic_launcher); // Add the select button AB to the dialog box. setNegativeButton ("OK", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "you have exited the application", Toast. LENGTH_SHORT ). show () ;}}); // the priority set by setNeutraButton is higher than that set by setNegativeButton. setNeutralButton ("cancel", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "you have canceled exiting the application", Toast. LENGTH_SHORT ). show () ;}}); // displays AB in the dialog box. show ();}}
2) Another method:
New AlertDialog. Builder (this). setTitle ("Tip:"). setMessage ("Are you sure? "). SetNegativeButton ("OK", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (Act2.this, "you have exited the application", Toast. LENGTH_SHORT ). show ();}}). setNeutralButton ("cancel", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (Act2.this, "you have canceled exiting this application", Toast. LENGTH_SHORT ). show ();}}). show ();
3) running effect:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051431523-0.jpg "title =" Capture. JPG "/>
2. ProgressDialog
1) MainActivity. java
Package com. example. l0826_progressdialog; import android. app. activity; import android. app. progressDialog; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {private ProgressDialog pd; private View view; private LayoutInflater lf; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); lf = (LayoutInflater) getSystemService (LAYOUT_INFLATER_SERVICE); view = lf. inflate (R. layout. cell, null); pd = new ProgressDialog (this); pd. setView (view); pd. setTitle ("Tip:"); pd. setMessage ("downloading"); pd. setMax (1, 1000); pd. setButton ("return", new OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "download completed", Toast. LENGTH_SHORT ). show () ;}}); // if this clause is not set to effect 1, set the result to effect 2 pd. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); pd. show ();}}
2) running effect 1. The setProgressStyle method is not set:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/105143G30-1.jpg "title =" Capture 1.JPG"/>
3) Running Effect 2. The setPrograssStyle method is set.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051435634-2.jpg "title =" Capture. JPG "/>
3. DatePickerDialog
1) MainActivity. java
Package com. example. l0826_datepickerdailog; import android. app. activity; import android. app. datePickerDialog; import android. app. datePickerDialog. onDateSetListener; import android. app. timePickerDialog; import android. app. timePickerDialog. onTimeSetListener; import android. OS. bundle; import android. widget. datePicker; import android. widget. timePicker; import android. widget. toast; public class MainActivity extends Activity {private DatePickerDialog dpd; private timepickerditpd tpd; private int year, month, day, hour, minute, second; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); year = 2013; month = 7; day = 26; hour = 06; minute = 30; // use dpd = new DatePickerDialog (MainActivity. this, new OnDateSetListener () {@ Override public void onDateSet (DatePicker view, int year, int monthOfYear, int dayOfMonth) {Toast. makeText (MainActivity. this, year + "-" + month + "-" + day, Toast. LENGTH_SHORT ). show () ;}, year, month, day); dpd. setTitle ("Alarm Clock"); dpd. setMessage ("date setting"); dpd. show (); // use tpd = new TimePickerDialog (MainActivity. this, new OnTimeSetListener () {@ Override public void onTimeSet (TimePicker view, int hourOfDay, int minute) {Toast. makeText (MainActivity. this, hour + "/" + minute, Toast. LENGTH_SHORT ). show () ;}, hour, minute, true); tpd. show ();}}
2) running effect:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051436400-3.jpg "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051434604-4.jpg "title =" Capture 1.JPG"/>
Ii. Menu)
1. OptionsMenu option menu)
When you click the MENU key, it appears. Generally, at most six items are displayed below the screen. If there are more than six items, you can find them using the "more" option;
2. SubMenu)
Only basic menus can be expanded, but submenus cannot be nested. Icon display is not supported;
3. ContextMenu context menu)
A menu that appears when you long press an interface element on the screen. Icon display is not supported. Similar to the right-click menu of a common desktop, the context menu is displayed after two seconds. The context menu can be registered to any View object, such as a basic space, layout control, ListView, and a single item in the Spinner.
You need to override the onContextItemSelected method.
Example:
1) Example 1:
Package com. example. l0826_optionmenu; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. menuItem; import android. view. subMenu; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {menu. add (Menu. NONE, 1, Menu. NONE, "menu 1"); menu. add (Menu. NONE, 2, Menu. NONE, "menu 2"); menu. add (Menu. NONE, 3, Menu. NONE, "menu 3"); menu. add (Menu. NONE, 4, Menu. NONE, "menu 4"); menu. add (Menu. NONE, 5, Menu. NONE, "menu 5"); SubMenu submenu = menu. addSubMenu (Menu. NONE, 6, Menu. NONE, "menu 6"); submenu. setHeaderIcon (R. drawable. a1); submenu. setIcon (R. drawable. a2); submenu. add (Menu. NONE, 7, Menu. NONE, "sub menu 7"); submenu. add (Menu. NONE, 8, Menu. NONE, "sub menu 8"); submenu. add (Menu. NONE, 9, Menu. NONE, "sub-menu 9"); return true;} @ Override public boolean onOptionsItemSelected (MenuItem item) {switch (item. getItemId () {case 1: Toast. makeText (MainActivity. this, "you have clicked menu 1", Toast. LENGTH_SHORT ). show (); break; case 2: Toast. makeText (MainActivity. this, "you have clicked menu 2", Toast. LENGTH_SHORT ). show (); break; case 3: Toast. makeText (MainActivity. this, "you have clicked menu 3", Toast. LENGTH_SHORT ). show (); break; case 4: Toast. makeText (MainActivity. this, "you have clicked menu 4", Toast. LENGTH_SHORT ). show (); break; case 5: Toast. makeText (MainActivity. this, "you have clicked menu 5", Toast. LENGTH_SHORT ). show (); break; case 6: Toast. makeText (MainActivity. this, "you have clicked menu 6", Toast. LENGTH_SHORT ). show (); break; case 7: Toast. makeText (MainActivity. this, "you have clicked menu 7", Toast. LENGTH_SHORT ). show (); break; case 8: Toast. makeText (MainActivity. this, "you have clicked menu 8", Toast. LENGTH_SHORT ). show (); break; case 9: Toast. makeText (MainActivity. this, "you have clicked menu 10", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onOptionsItemSelected (item );}}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051432958-5.jpg "style =" float: none; "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051431638-6.jpg "style =" float: none; "title =" Capture 1.JPG"/>
2) Example 2:
Package com. example. l0826_optionmenu; import android. OS. bundle; import android. app. activity; import android. view. contextMenu; import android. view. menu; import android. view. menuItem; import android. view. subMenu; import android. view. view; import android. view. contextMenu. contextMenuInfo; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {private TextView TV; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TV = (TextView) findViewById (R. id. TV); registerForContextMenu (TV) ;}@ Override public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo) {menu. add (Menu. NONE, 1, Menu. NONE, "menu 1"); menu. add (Menu. NONE, 2, Menu. NONE, "menu 2"); menu. add (Menu. NONE, 3, Menu. NONE, "menu 3"); menu. add (Menu. NONE, 4, Menu. NONE, "menu 4"); menu. add (Menu. NONE, 5, Menu. NONE, "menu 5"); SubMenu submenu = menu. addSubMenu (Menu. NONE, 6, Menu. NONE, "sub menu"); submenu. setHeaderIcon (R. drawable. a1); submenu. setIcon (R. drawable. a2); submenu. add (Menu. NONE, 7, Menu. NONE, "sub menu 1"); submenu. add (Menu. NONE, 8, Menu. NONE, "sub menu 2"); submenu. add (Menu. NONE, 9, Menu. NONE, "sub-menu 3"); super. onCreateContextMenu (menu, v, menuInfo) ;}@ Override public boolean onContextItemSelected (MenuItem item) {switch (item. getItemId () {case 1: Toast. makeText (MainActivity. this, "you have clicked menu 1", Toast. LENGTH_SHORT ). show (); break; case 2: Toast. makeText (MainActivity. this, "you have clicked menu 2", Toast. LENGTH_SHORT ). show (); break; case 3: Toast. makeText (MainActivity. this, "you have clicked menu 3", Toast. LENGTH_SHORT ). show (); break; case 4: Toast. makeText (MainActivity. this, "you have clicked menu 4", Toast. LENGTH_SHORT ). show (); break; case 5: Toast. makeText (MainActivity. this, "you have clicked menu 5", Toast. LENGTH_SHORT ). show (); break; case 6: Toast. makeText (MainActivity. this, "you have clicked menu 6", Toast. LENGTH_SHORT ). show (); break; case 7: Toast. makeText (MainActivity. this, "you have clicked menu 7", Toast. LENGTH_SHORT ). show (); break; case 8: Toast. makeText (MainActivity. this, "you have clicked menu 8", Toast. LENGTH_SHORT ). show (); break; case 9: Toast. makeText (MainActivity. this, "you have clicked menu 10", Toast. LENGTH_SHORT ). show (); break; default: break;} return super. onContextItemSelected (item );}}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10514322W-7.jpg "style =" float: none; "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051434W6-8.jpg "style =" float: none; "title =" Capture 1.JPG"/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/105143DE-9.jpg "style =" float: none; "title =" Capture 2.JPG"/>
3. Notification bar)
The Code is as follows:
Package com. example. l0826_notification; import android. app. activity; import android. app. notification; import android. app. icationicationmanager; import android. app. pendingIntent; import android. content. context; import android. content. intent; import android. OS. bundle; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // create icationicationmanager to manage Notification NotificationManager manager = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE); // create the Notification object. You can directly set the icon, title, and Id Notification = new notification (R. drawable. ic_launcher, "Weather prompt", 0); PendingIntent pending = PendingIntent. getActivity (MainActivity. this, 0, getIntent (), Intent. FLAG_ACTIVITY_NEW_TASK); notification. setLatestEventInfo (MainActivity. this, "Harbin Weather Forecast", "Clear, warm", pending); manager. notify (0, notification) ;}@ Override protected void onRestart () {icationicationmanager manager = (icationicationmanager) getSystemService (Context. NOTIFICATION_SERVICE); manager. cancel (0); super. onRestart ();}}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051431294-10.jpg "style =" float: none; "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051436023-11.jpg "style =" float: none; "title =" Capture 1.JPG"/>
Iv. TagHost navigation labels)
1. Method 1: Inherit the TabActivity navigation label
Package com. example. l0826_tabhost; import android. app. tabActivity; import android. content. intent; import android.net. uri; import android. OS. bundle; import android. support. v4.app. icationicationcompat. action; import android. view. layoutInflater; import android. widget. tabHost; public class MainActivity extends TabActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); TabHost th = getTabHost (); LayoutInflater. from (this ). inflate (R. layout. activity_main, th. getTabContentView (), true); th. addTab (th. newTabSpec ("Tab1 "). setIndicator ("Navigation 1 "). setContent (new Intent (MainActivity. this, Act2.class); th. addTab (th. newTabSpec ("Tab2 "). setIndicator ("navigation 2 "). setContent (R. id. layout ));}}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1051433105-12.jpg "title =" Capture. JPG "/>
2. Method 2: Customize TabHost
package com.example.jiuzhouzhanji;import android.app.Activity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;import android.widget.TabWidget;public class Act_001 extends Activity{ private TabHost tabHost; private TabWidget tabWidget; private int imageA[]={R.drawable.b007,R.drawable.b008,R.drawable.b006,R.drawable.b005}; private int imageB[]={R.drawable.b007_1,R.drawable.b008_1,R.drawable.b006_1,R.drawable.b005_1}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act_001); tabHost=(TabHost)findViewById(R.id.tabhost); tabHost.setup(); tabWidget=tabHost.getTabWidget(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator(createTabView(tabHost, imageA[0])).setContent(R.id.tab1)); tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator(createTabView(tabHost, imageA[1])).setContent(R.id.tab2)); tabHost.addTab(tabHost.newTabSpec("tab3") .setIndicator(createTabView(tabHost, imageA[2])).setContent(R.id.tab3)); tabHost.addTab(tabHost.newTabSpec("tab4") .setIndicator(createTabView(tabHost, imageA[3])).setContent(R.id.tab4)); showSpec(); tabHost.setOnTabChangedListener(new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { showSpec(); } }); } public void showSpec(){ for(int i=0;i<tabWidget.getChildCount();i++){ View view=tabWidget.getChildAt(i); if(tabHost.getCurrentTab()==i){ view.setBackgroundResource(imageA[i]); }else { view.setBackgroundResource(imageB[i]); } } } public View createTabView (TabHost tabHost,int tabImage){ View tabView=LayoutInflater.from(this).inflate(R.layout.cell, null); ImageView iv=(ImageView)tabView.findViewById(R.id.iv); iv.setBackgroundResource(tabImage); return tabView; }}
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/105143FG-13.jpg "title =" Capture. JPG "/>
This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1283027