Androidstudio button to exit the program

Source: Internet
Author: User

Androidstudio 3.1.4

1. Create a new project, the project name is button, the interface is Activity_button.xml

2. Open Activity_button.xml

3. Click the HelloWorld tab and press DELETE to delete

4. Left component Bar Select Common-button

5. Drag the button component to the interface, about the middle position

6. Modify properties on the right

The onclick is the mouse click event receive function, the following code needs to use

7. When the interface finishes

8. Double-click the java\com. Package name. button with no parentheses behind

9. Code edit box Write code

10. The code is as follows

ShowDialog is the mouse down event receive function, which is triggered when the mouse button is clicked

1      Public voidShowDialog (view view)2     {3         //to define a new dialog box object4Alertdialog.builder alertdialogbuilder=NewAlertdialog.builder ( This);5         //Setting dialog box prompt content6Alertdialogbuilder.setmessage ("Are you sure you want to quit the program?" ");7         //Define a dialog box 2 button titles and functions to accept events8Alertdialogbuilder.setpositivebutton ("OK", click1);9Alertdialogbuilder.setnegativebutton ("Cancel", Click2);Ten         //Create and display a dialog box OneAlertdialog alertdialog1=alertdialogbuilder.create (); A alertdialog1.show (); -  -}

Listen for the dialog button events defined above

1     PrivateDialoginterface.onclicklistener click1=NewDialoginterface.onclicklistener ()2     {3         //the token is used to enhance the check of the program at compile time, and if the method is not a method that overrides the parent class, the compiler will report an error at compile time. 4 @Override5 6          Public voidOnClick (Dialoginterface arg0,intarg1)7         {8             //executes the end process when the button click1 is pressed9 android.os.Process.killProcess (Android.os.Process.myPid ());Ten         } One     }; A  -     PrivateDialoginterface.onclicklistener click2=NewDialoginterface.onclicklistener () -     { the @Override -          Public voidOnClick (Dialoginterface arg0,intarg1) -         { -             //Cancel the operation when the button Click2 is pressed + Arg0.cancel (); -         } +     }; A  at}

11. When you are finished writing, click AvD Manager in the upper right corner

12. Select the Downloaded emulator

13. If no download simulator is available, click on Create Virtual device below to download

14. Click the Play button under actions to run

15. The appearance after successful opening

16. Click Run ' App ' on the menu bar to debug

17. Select the simulator model you just opened, click OK

18. The program will compile, if no errors in the simulator will display properly

19. Click the button of the software in the debug

20. Click OK, the software ends properly, proves the debugging is successful

21. You can choose the package with the signature, and program it into APK file! lucky~

Androidstudio button to exit the program

Related Article

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.