dialog box
- dialog box
- Brief introduction
- Body
- Extended Reading
Target group: Android Beginner without foundation
Knowledge Points: Creation and invocation of Dialogfragment
Goal: Display a dialog box with two buttons in the page
Brief introduction
Body
1. dialog box is a small window that appears in front of the user, which allows the user to make some immediate actions. In the Android version after 3.0, the unified recommendation is to use dialogfragment to achieve, for the lower version of Android, first we need to add a reference to the support V4 package in Build.gradle, the code is as follows:
dependencies { ... ‘com.android.support:support-v4:21.0.3‘ ...}
-
You can also select the open Module settings-dependencies-in the project and click on the right plus-library dependency-select Appcompat-v4 to add
-
If the Android version is above 3.0, you do not need to consider this
-
The following example is the SUPPORT-V4 version
2. Create a fragmentactivity page named Mainactivity and create two methods to correspond to the Click events of the Dialog button:
publicvoiddoPositiveClick() { Log.i("FragmentAlertDialog""Positive click!"); } publicvoiddoNegativeClick() { Log.i("FragmentAlertDialog""Negative click!"); }
3. Create a dialogfragment page, named Myalertdialogfragment, and implement its Oncreatedialog method, with the following code:
ImportAndroid.app.AlertDialog;ImportAndroid.app.Dialog;ImportAndroid.content.DialogInterface;ImportAndroid.os.Bundle;ImportAndroid.support.v4.app.DialogFragment; Public class myalertdialogfragment extends dialogfragment { Public StaticMyalertdialogfragmentnewinstance(String title) {//Create a new dialog box and get the title value from the calling pageMyalertdialogfragment Frag =NewMyalertdialogfragment (); Bundle args =NewBundle (); Args.putstring ("title", title); Frag.setarguments (args);returnFrag }@Override PublicDialogOncreatedialog(Bundle savedinstancestate) {/** Gets the title value and creates a dialog box that contains two buttons when two buttons are clicked, the Dopositiveclick and Donegativeclick methods of the Mainactivity page are called respectively **/String title = Getarguments (). GetString ("title");return NewAlertdialog.builder (Getactivity ()). SetIcon (R.drawable.ic_launcher). Settitle (title) . Setpositivebutton ("OK",NewDialoginterface.onclicklistener () { Public void OnClick(Dialoginterface Dialog,intWhichbutton) {((mainactivity) getactivity ()). Dopositiveclick (); }}). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { Public void OnClick(Dialoginterface Dialog,intWhichbutton) {((mainactivity) getactivity ()). Donegativeclick (); }}). Create (); }}
4. Back to Mainactivity, use the code to invoke the Dialogfragment code as follows:
@Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showDialog(); } publicvoidshowDialog() { DialogFragment newFragment = MyAlertDialogFragment.newInstance("两个按钮的对话框"); "alert"); }
- Of course, the fragment can be used as a member of the view structure to invoke the Dialogfragment
FragmentTransaction ft = getFragmentManager().beginTransaction();DialogFragment newFragment = MyDialogFragment.newInstance();ft.add(R.id.embedded, newFragment);ft.commit();
Extended Reading
- Dialogfragment's Official API
- Dialogs's Official API
Android Basics (17)