1. Dialog (Dialog box ):
Some dialogs are used in android development to interact with users.
You must use the AlertDialog. Builder class to create a dialog box.
Common Methods:
SetTille (): Set the title for the dialog box;
SetIcon (): Set the icon for the dialog box
SetMessage (): Set the prompt information in the dialog box.
SetItems (): Set a list to be displayed in the dialog box. It is generally used to display several commands.
SetSingleChoiceItems (): The Setting dialog box displays a single-choice List.
SetMultiChoiceItems (): used to set a series of check boxes displayed in the dialog box.
SetPositiveButton (): add the "yes" button to the dialog box.
SetNegativeButton (): add the "No" button to the dialog box.
Dialog layout file:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1.31" android: text = "username:"/> <EditText android: id = "@ + id/editText1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1.31" android: EMS = "10"/> </LinearLayout> <LinearLayout android: layout_width = "fill_parent" android: layout_height = "wrap_content"> <TextView android: id = "@ + id/textView2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1.31" android: text = "Password:"/> <EditText android: id = "@ + id/editText2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_weight = "1.31" android: password = "true" android: EMS = "10"/> </LinearLayout>
Main Interface layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
Main Code:
Public class MainActivity extends Activity {private TextView text; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); text = (TextView) findViewById (R. id. text); AlertDialog dialog = new AlertDialog. builder (MainActivity. this ). setTitle ("Logon prompt") // set the title. setMessage ("Please log in") // sets the content. setPositiveButton ("OK", // set the OK button new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// click "OK" to switch to the login box LayoutInflater factory = LayoutInflater. from (MainActivity. this); // get the custom login box View dv = factory. inflate (R. layout. activity_main, null); AlertDialog dlg = new AlertDialog. builder (MainActivity. this ). setTitle ("login box "). setView (dv) // set the style of the custom dialog box. setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stubtext. setText ("You have logged on successfully ");}}). setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stubtext. setText ("You failed to log on ");}}). create (); dlg. show ();}}). setNegativeButton ("exit", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface arg0, int arg1) {// TODO Auto-generated method stubMainActivity. this. finish ();}}). create (); dialog. show () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
Running interface:
Click OK:
·
2. ProgressBar (progress bar ):
The android system provides two types of progress bar styles, including the progress bar (progress-BarStyleHorizontal) and the progress bar (progressBarStyleLarge). The progress bar can be applied when the application loads resources, prompting you to wait.
Layout:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <TextView android: id = "@ + id/textView1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "click the button"/> <ProgressBar android: id = "@ + id/p1" style = "? Android: attr/progressBarStyleHorizontal "android: layout_width =" 200dp "android: layout_height =" wrap_content "android: visibility =" gone "/> <ProgressBar android: id = "@ + id/p2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: max = "100" style = "? Android: attr/progressBarStyleLarge "android: progress =" 50 "android: secondaryProgress =" 70 "android: visibility =" gone "/> <Button android: id = "@ + id/b1" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> </LinearLayout>
If Id is p1, it indicates a long progress bar. If Id is p2, it indicates a circular progress bar. If style is set, it indicates the style of the progress bar.
Style = "? Android: attr/progressBarStyleHorizontal "long progress bar
Style = "? Android: attr/progressBarStyleLarge "Oversized circular ProgressBar
Style = "? Android: attr/progressBarStyleSmall "Small circular ProgressBar
Style = "? Android: attr/progressBarStyleSmallTitle "title Type circular ProgressBar
Main Code:
Public class MainActivity extends Activity {private ProgressBar p1; private ProgressBar p2; private Button b1; protected static final int STOP_NOTIFIER = 0x108; protected static final int THREADING_NOTIFIER = 0x109; public int counter = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // sets the Window mode requestWindowFeature (Window. FEATURE_PROGRESS); setProgressBarV Isibility (true); setContentView (R. layout. activity_main); p1 = (ProgressBar) findViewById (R. id. p1); p2 = (ProgressBar) findViewById (R. id. p2); b1 = (Button) findViewById (R. id. b1); b1.setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// set ProgressBar to visible p1.setVisibility (View. VISIBLE); p2.setVisibility (View. VISIBLE); // set the maximum p1.setMax (100) of ProgressBar; // set the current value of ProgressBar p1.setP Rogress (0); p2.setProgress (0); // change the value of ProgressBar through a Thread. new Thread (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubfor (int I = 0; I <10; I ++) {try {counter = (I + 1) * 20; Thread. sleep (1000); if (I = 4) {Message m = new Message (); m. what = MainActivity. STOP_NOTIFIER; MainActivity. this. handler. sendMessage (m); break;} else {Message m = new Message (); m. what = MainActivity. THREADING_NOTIFIER; Ma InActivity. this. handler. sendMessage (m) ;}} catch (Exception e) {e. printStackTrace ();}}}}). start () ;}}) ;}// the updated page displays Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stub switch (msg. what) {// ProgressBar is already the maximum value case MainActivity. STOP_NOTIFIER: p1.setVisibility (View. GONE); p2.setVisibility (View. GONE); // interrupt Thread. currentThread (). interrupt (); break; case THREADING_NOTIFIER: if (! Thread. currentThread (). isInterrupted () {p1.setProgress (counter); p2.setProgress (counter); // set a progress bar value setProgress (counter * 100) in the title bar ); // set a progress bar value setSecondaryProgress (counter * 100);} break;} super. handleMessage (msg) ;}}; @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
Running image:
Zookeeper