Use Dialog Box -- dialog, use Dialog box dialog
A dialog box is a pop-up window that prompts users and interacts with users.
Create Activity dialog box
Use the Activity simulation dialog box. This is relatively simple, mainly because the Dialog topic of the Activity is used.
Create a DialogActivity and register it in AndroidManifest.
Change the topic of DialogActivity:
<activity android:theme="@android:style/Theme.Dialog" android:name="com.whathecode.usingdialog.DialogActivity" android:label="@string/title_activity_dialog" ></activity>
DialogActivity code example:
Package com. whathecode. usingdialog; import android. app. activity; import android. OS. bundle; import android. view. view; public class DialogActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_dialog);} // used to close this Activity public void close (View view) {finish ();}}
DialogActivity layout file:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: padding = "5dp" android: orientation = "vertical" android: gravity = "center_vertical | center_horizontal" tools: context = ". dialogActivity "> <TextView android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text =" this is an Activity-based Dialog "/> <LinearLayout android: layout_marginTop = "10dp" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "horizontal"> <Button android: id = "@ + id/confirm" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "OK" android: onClick = "close"/> <Button android: id = "@ + id/cancel" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "cancel" android: onClick = "close"/> </LinearLayout>
MainActivity code:
package com.whathecode.usingdialog;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.view.View;public class MainActivity extends FragmentActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void openActivityDialog(View view) { Intent intent = new Intent(this, DialogActivity.class); startActivity(intent); }}
Running effect:
Package com. whathecode. usingdialog; import android. app. activity; import android. app. alertDialog; import android. app. dialog; import android. app. progressDialog; import android. content. dialogInterface; import android. content. intent; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. widget. toast; public class MainActivity extends Activity {priv Ate static final int SINGLE_CHOICE_DIALOG = 0; private static final int MULTI_CHOICE_DIALOG = 1; private static final int PROGRESS_DIALOG = 2; protected static final int MAX_PROGRESS = 30; private CharSequence items [] = new String [] {"apple", "google", "microsoft"}; private boolean checkedItems [] = new boolean [3]; private Handler progressHandler; private int progress; protected ProgressDialog pr OgressDialog; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); progressHandler = new Handler () {@ Override public void handleMessage (Message msg) {super. handleMessage (msg); if (progress> = MAX_PROGRESS) {progressDialog. dismiss (); // close progressDialog} else {progress ++; // progress bar and 1 progressDialog. incrementProgre SsBy (1); // as long as the current progress is less than the total progress, the message progressHandler is sent every 100 milliseconds. sendEmptyMessageDelayed (0,100) ;}};} public void openActivityDialog (View view) {Intent intent = new Intent (this, DialogActivity. class); startActivity (intent);} // display single-choice dialog box public void openSinglechoiceDialog (View view) {showDialog (SINGLE_CHOICE_DIALOG);} // display multiple-choice dialog box public void openMultichoiceDialog (View view) {showDialog (MULTI_CHOICE_DIALO G) ;}// public void openProgressDialog (View view) {showDialog (PROGRESS_DIALOG); progress = 0; progressDialog. setProgress (0); progressHandler. sendEmptyMessage (0) ;}@ Override @ Deprecated protected Dialog onCreateDialog (int id) {switch (id) {case when: return createSingleChoiceDialog (); case MULTI_CHOICE_DIALOG: return when (); case PROGRESS_DIALOG: retu Rn createProgressDialog (); default: break;} return null;}/*** create single choice Dialog box **/public Dialog createSingleChoiceDialog () {return new AlertDialog. builder (this ). setTitle ("single choice dialog box") // set the dialog box title. setNegativeButton ("cancel", null) // sets the cancel button. setPositiveButton ("OK", null) // set "OK" to press. setSingleChoiceItems (items, 0, // bind Data new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, Int which) {Toast. makeText (getBaseContext (), items [which]. toString (), Toast. LENGTH_SHORT ). show ();}}). create ();}/*** create multiple choice Dialog box **/public Dialog createMultichoiceDialog () {return new AlertDialog. builder (this ). setTitle ("Multi-choice dialog box") // set the dialog box title. setNegativeButton ("cancel", null) // sets the cancel button. setPositiveButton ("OK", null) // sets the OK button. setMultiChoiceItems (items, checkedItems, // bind data to new DialogInterface. onMulti ChoiceClickListener () {@ Override public void onClick (DialogInterface dialog, int which, boolean isChecked) {Toast. makeText (getBaseContext (), isChecked? Items [which] + "check": items [which] + "uncheck", Toast. LENGTH_SHORT ). show ();}}). create ();}/*** create a Dialog box with a progress bar **/public Dialog createProgressDialog () {progressDialog = new ProgressDialog (this); progressDialog. setTitle ("Download Dialog Box"); progressDialog. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); progressDialog. setMax (MAX_PROGRESS); progressDialog. setButton (DialogInterface. BUTTON_POSITIVE, "OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}}); progressDialog. setButton (DialogInterface. BUTTON_NEGATIVE, "cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {}}); return progressDialog ;}}
Running effect:
It is hard to understand here or ProgressDialog, because it needs to increase the progress. Here we send a message to the Activity thread,
You can use progressDialog. incrementProgressBy (1) to increment the progress bar.