Android AlertDialog warning dialog box implementation.

Source: Internet
Author: User

Today, I read about the implementation of the Android AlertDialog warning dialog box, and wrote a query item. I will share it below.
 
The dialog box notification is mainly used to post the code when the user needs to make a confirmation or other selection.

Strings. xml

[Html]
01. <? Xml version = "1.0" encoding = "UTF-8"?>
02. <resources>
03.
04. <string name = "app_name"> FileManage </string>
05. <string name = "hello_world"> Hello world! </String>
06. <string name = "menu_settings"> Settings </string>
07. <string name = "button"> dialog box </string>
08.
09. </resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>

<String name = "app_name"> FileManage </string>
<String name = "hello_world"> Hello world! </String>
<String name = "menu_settings"> Settings </string>
<String name = "button"> dialog box </string>

</Resources>
Main. xml

 

[Html]
01. <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
02. xmlns: tools = "http://schemas.android.com/tools"
03. android: layout_width = "match_parent"
04. android: layout_height = "match_parent"
05. tools: context = ". MainActivity">
06.
07. <Button
08. android: layout_width = "wrap_content"
09. android: layout_height = "wrap_content"
10. android: text = "@ string/button"
11. android: id = "@ + id/button"
12./>
13.
14. </RelativeLayout>
<RelativeLayout 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"
Tools: context = ". MainActivity">

<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/button"
Android: id = "@ + id/button"
/>

</RelativeLayout>
The following is the java code

MainActivity. java

[Java]
01. package com. example. filemanage;
02.
03. import android. app. Activity;
04. import android. app. AlertDialog;
05. import android. content. DialogInterface;
06. import android. content. Intent;
07. import android.net. Uri;
08. import android. OS. Bundle;
09. import android. view. View;
10. import android. widget. Button;
11. import android. view. Menu;
12.
13. public class MainActivity extends Activity {
14.
15. @ Override
16. protected void onCreate (Bundle savedInstanceState ){
17. super. onCreate (savedInstanceState );
18. setContentView (R. layout. activity_main );
19.
20. Button button = (Button) findViewById (R. id. button );
21. button. setOnClickListener (new View. OnClickListener (){
22. @ Override
23. public void onClick (View v ){
24. AlertDialog. Builder builder =
25. new
26. AlertDialog. Builder (MainActivity. this );
27. builder. setTitle ("hopean.com ")
28. setMessage ("are you sure you want to visit our website? ")
29 .. setCancelable (false)
30 .. setPositiveButton ("OK ",
31. new DialogInterface. OnClickListener (){
32. public void onClick (DialogInterface dialog, int id)
33 .{
34. // create an intent to access the http://www.hopean.com website,
35. // The intention will tell the system to open a browser and access the website.
36. Intent intent =
37. new Intent (Intent. ACTION_VIEW, Uri. parse ("http://www.hopean.com "));
38. startActivity (intent );
39 .}
40 .})
41 .. setNegativeButton ("cancel ",
42. new DialogInterface. OnClickListener (){
43. public void onClick (DialogInterface dialog, int id)
44 .{
45. dialog. cancel (); // Delete dialog box
46 .}
47 .});
48. AlertDialog alert = builder. create (); // create dialog box
49. alert. show (); // display dialog box
50 .}
51 .});
52 .}
53.
54. @ Override
55. public boolean onCreateOptionsMenu (Menu menu ){
56. // Inflate the menu; this adds items to the action bar if it is present.
57. getMenuInflater (). inflate (R. menu. activity_main, menu );
58. return true;
59 .}
60.

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.