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

Android经常会用到AlertDialog,把内容使用AlertDialog结合列表的形式显示出来,然后我们点击得到点击的信息。 这里可以使用两层的AlertDialog来实现

          1:我们现在xml文件中定义一个要显示内容列表数组

          2:在Activity中使用 String[] items = getResources().getStringArray(R.array.item);

          3:增添点击事件,使用Alertdialog.builder  千万不能忘了最后进行show()哦  

          直接看截图的效果:

源代码:

[java]

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文件内容:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyjwjs.html