今天看了一下Android AlertDialog警告對話方塊實現相關知識,查詢資料自己編寫了一個,下面就分享一下
對話方塊通知主要是當需要使用者做出確定或其他某種選擇時使用. 貼出代碼
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">彈出對話方塊</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">彈出對話方塊</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>
下面是java代碼
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("你確定要訪問 我們網站嗎?")
29. .setCancelable(false)
30. .setPositiveButton("確定",
31. new DialogInterface.OnClickListener() {
32. public void onClick(DialogInterface dialog, int id)
33. {
34. //建立一個訪問“http://www.hopean.com”網站的意圖,
35. //該意圖會告知系統開啟瀏覽器,並訪問該網址。
36. Intent intent =
37. new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.hopean.com"));
38. startActivity(intent);
39. }
40. })
41. .setNegativeButton("取消",
42. new DialogInterface.OnClickListener() {
43. public void onClick(DialogInterface dialog, int id)
44. {
45. dialog.cancel(); //刪除對話方塊
46. }
47. });
48. AlertDialog alert = builder.create();//建立對話方塊
49. alert.show();//顯示對話方塊
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.