Android Basics (17)

Source: Internet
Author: User

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
    • Introduction to the dialog box is ready for import

    • Simple invocation of the Dialogfragment class

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
    1. Dialogfragment's Official API
    2. Dialogs's Official API

Android Basics (17)

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.