第一章:具有选择功能对话框
效果:
main.xml
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget0"android:layout_width="fill_parent"android:layout_height="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android"><Buttonandroid: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() {@Overridepublic 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() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).show();}/**为选择对话框设置取消按钮事件*/}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).show(); } }); }}