您的位置:首页 > 移动开发 > Android开发

Android中用两层AlertDialog来进行弹出选择框信息选择

2012-01-06 19:51 459 查看
在Android经常会用到AlertDialog,把内容使用AlertDialog结合列表的形式显示出来,然后我们点击得到点击的信息。 这里可以使用两层的AlertDialog来实现
1:我们现在xml文件中定义一个要显示内容列表数组
2:在Activity中使用 String[] items = getResources().getStringArray(R.array.item);
3:增添点击事件,使用Alertdialog.builder 千万不能忘了最后进行show()哦
直接看截图的效果:





源代码:
package com.jiangqq.alertdialog;  import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;  /**  * 使用AlertDialog进行选择功能  *   * @author jiangqq  *   */ public class AlertDialogActivity extends Activity { 	private Button btn;  	@Override 	public void onCreate(Bundle savedInstanceState) { 		super.onCreate(savedInstanceState); 		setContentView(R.layout.main);  		btn = (Button) findViewById(R.id.btn); 		btn.setOnClickListener(new OnClickListener() {  			public void onClick(View v) { 				final String[] items = getResources().getStringArray( 						R.array.item); 				new AlertDialog.Builder(AlertDialogActivity.this) 						.setTitle("请点击选择") 						.setItems(items, new DialogInterface.OnClickListener() {  							public void onClick(DialogInterface dialog, 									int which) { 								new AlertDialog.Builder( 										AlertDialogActivity.this) 										.setTitle("你选择了:" + items[which]) 										.setMessage("点击选择操作") 										.setPositiveButton( 												"确定", 												new DialogInterface.OnClickListener() {  													public void onClick( 															DialogInterface dialog, 															int which) { 														// 这里是你点击确定之后可以进行的操作 													} 												}) 										.setNegativeButton( 												"取消", 												new DialogInterface.OnClickListener() {  													public void onClick( 															DialogInterface dialog, 															int which) { 														// 这里点击取消之后可以进行的操作 													} 												}).show(); 							} 						}).show(); 			} 		}); 	} }


string.xml文件内容:
<?xml version="1.0" encoding="utf-8"?> <resources>      <string name="hello">Hello World, AlertDialogActivity!</string>     <string name="app_name">Hello World, AlertDialogActivity</string>     <string name="btn_name">点击弹出AlertDialog</string>      <string-array name="item">         <item>第一个选择</item>         <item>第二个选择</item>         <item>第三个选择</item>         <item>第四个选择</item>     </string-array>  </resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息