一般的对话框只有一条message,怎么让对话框列出很多选项,并且可以选中其中的一个呢?那就要在对话框中加入数组,使之具有选择功能。
现在string中加入数组,代码如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="str_ok">确认</string> <array name="items_irdc_dialog"> <item>你是帅哥</item> <item>你是美女</item> <item>无法识别</item> </array> </resources>
逻辑代码如下:
public class ShowAlertDialogActivity extends Activity { public Button mButton1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton1 =(Button) findViewById(R.id.myButton1); mButton1.setOnClickListener(myShowAlertDialog); } Button.OnClickListener myShowAlertDialog = new Button.OnClickListener() { public void onClick(View arg0) { new AlertDialog.Builder(ShowAlertDialogActivity.this) .setTitle(R.string.str_alert_title) .setItems(R.array.items_irdc_dialog,//加入数组 new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichcountry) { String strDialogBody = getString(R.string.str_alert_body); String[] aryShop = getResources().getStringArray(R.array.items_irdc_dialog); new AlertDialog.Builder(ShowAlertDialogActivity.this) .setMessage(strDialogBody + aryShop[whichcountry]) .setNeutralButton(R.string.str_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .show(); } }) .setNegativeButton("", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface d, int which) { d.dismiss(); } }) .show(); } }; }
重点就在加入数组上,搞定!收工!