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