package yu.android;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
public class Alertdialog extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void clickHandler(View v) {
new AlertDialog.Builder(this).setTitle("訊息").setItems(R.array.strArray,
//建造第一個彈出對話框
new DialogInterface.OnClickListener() {
//對話框內每個項目做 click 事件
public void onClick(DialogInterface dialog, int which) {
String[] weekArray = getResources().getStringArray(
R.array.strArray);
new AlertDialog.Builder(Alertdialog.this).setMessage(
weekArray[which]).show();
//which 指到您所選取的 index
};
}).show();
}
}
下面是 /value/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Alertdialog!</string>
<string name="app_name">Alert_Dialog</string>
<string-array name="strArray">
<item>星期一</item>
<item>星期二</item>
<item>星期三</item>
<item>星期四</item>
<item>星期五</item>
<item>星期六</item>
<item>星期日</item>
</string-array>
</resources>
下面是 /layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/myBt" android:text="按鈕" android:layout_width="fill_parent" android:layout_height="50dip"
android:onClick="clickHandler"/>
</LinearLayout>
請先 登入 以發表留言。