第一章:具有選擇功能對話框

效果:


[img]http://dl.iteye.com/upload/attachment/386331/6c8835e2-b50e-33cc-8422-635dea34a9c5.jpg[/img]


[img]http://dl.iteye.com/upload/attachment/386333/ed41d2c3-1057-3ae0-940b-4cbe5bb4fc73.jpg[/img]


main.xml


<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/selcet"
android:layout_width="95px"
android:layout_height="wrap_content"
android:text="選擇"
android:layout_x="123px"
android:layout_y="189px"
>
</Button>
</AbsoluteLayout>




strings.xml



<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AlertDialogTest!</string>
<string name="alert_select_title">請選擇內容</string>
<string name="dialog_title">您選擇的是:</string>
<string name="ok">確定</string>
<string name="cancel">取消</string>
<array name="alert_select_array">
<item>串串香</item>
<item>烤肉</item>
<item>火鍋</item>
<item>肯德基</item>
</array>
<string name="app_name">AlertDialogTest</string>
</resources>






package alert.dialog.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AlertDialogTest extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**載入main.xml */
setContentView(R.layout.main);
/**通過id找到button組件*/
Button bt=(Button)findViewById(R.id.selcet);
/**設置button按鈕點擊事件*/
bt.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
/**設置對話框標題,內容點擊事件*/
new AlertDialog.Builder(AlertDialogTest.this).setTitle(R.string.alert_select_title).setItems(R.array.alert_select_array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/**獲得strings.xml name="dialog_title"內容*/
CharSequence strDialog=getString(R.string.dialog_title);
/**獲得strings.xml name="alert_select_array"數組內容*/
String[] str=getResources().getStringArray(R.array.alert_select_array);
new AlertDialog.Builder(AlertDialogTest.this).setMessage(strDialog+ str[which]).setNegativeButton(R.string.ok, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
}
/**爲選擇對話框設置取消按鈕事件*/
}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
});
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章