1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout
3
xmlns:android="http://schemas.android.com/apk/res/android"
4
android:orientation="vertical"
5
android:layout_width="fill_parent"
6
android:layout_height="fill_parent">
7
<TextView
8
android:layout_width="fill_parent"
9
android:layout_height="wrap_content"
10
android:text="@string/hello" />
11
<Button
12
android:id="@+id/button1"
13
android:layout_width="fill_parent"
14
android:layout_height="wrap_content"
15
android:text="單擊顯示彈出窗口"/>
16
</LinearLayout>
17

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

1
public class BeginAndroidActivity extends Activity {
2
private String tag = "Events";
3
private CharSequence[] items = {"Google","Apple","HTC"};
4
private boolean[] itemsChecked = new boolean[items.length];
5
6
@Override
7
public void onCreate(Bundle savedInstanceState) {
8
super.onCreate(savedInstanceState);
9
setContentView(R.layout.main);
10
Log.d(tag, "In the onCreate() event");
11
Button button = (Button) findViewById(R.id.button1);
12
button.setOnClickListener(new View.OnClickListener() {
13
public void onClick(View v) {
14
showDialog(0);
15
}
16
});
17
}
18
}
19

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

1
@Override
2
protected Dialog onCreateDialog(int id, Bundle args) {
3
switch (id) {
4
case 0:
5
return new AlertDialog.Builder(this)
6
.setIcon(R.drawable.icon)
7
.setTitle("這是一個(gè)Dialog")
8
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
9
public void onClick(DialogInterface dialog, int which) {
10
Toast.makeText(getBaseContext(), "OK Clicked!", Toast.LENGTH_SHORT).show();
11
}
12
})
13
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
14
public void onClick(DialogInterface dialog, int which) {
15
Toast.makeText(getBaseContext(), "Cancel Clicked!", Toast.LENGTH_SHORT).show();
16
}
17
})
18
.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
19
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
20
Toast.makeText(getBaseContext(), items[which] + (isChecked ? " checked!" :" unChecked!"), Toast.LENGTH_SHORT).show();
21
}
22
})
23
.create();
24
}
25
return null;
26
27
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27
