android 简单列表对话框(AlertDialog.Builder().setItems())

android 简单列表对话框(AlertDialog.Builder().setItems())   

android 简单列表对话框(AlertDialog.Builder().setItems())  

  

android 简单列表对话框(AlertDialog.Builder().setItems())  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.js.listdialogtest1;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
 
public class ListDialogTest1 extends Activity implements OnClickListener
{
 
    private Button btnListDialog;
 
    private String[] provinces = new String[] { "上海""北京""湖南""湖北""海南" };
 
  
 
    /** Called when the activity is first created. */
 
    @Override
 
    public void onCreate(Bundle savedInstanceState)
 
    {
 
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
 
  
 
        btnListDialog = (Button) findViewById(R.id.btnListDialog);
 
        btnListDialog.setOnClickListener(this);
 
    }
 
  
 
    @Override
 
    public void onClick(View v)
 
    {
 
        switch (v.getId())
 
        {
 
        case R.id.btnListDialog:
 
            showListDialog();
 
            break;
 
        }
 
  
 
    }
 
  
 
    private void showListDialog()
 
    {
 
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
 
        builder.setTitle("请选择省份");
 
  
 
        /**
 
         * 1、public Builder setItems(int itemsId, final OnClickListener
 
         * listener) itemsId表示字符串数组的资源ID,该资源指定的数组会显示在列表中。 2、public Builder
 
         * setItems(CharSequence[] items, final OnClickListener listener)
 
         * items表示用于显示在列表中的字符串数组
 
         */
 
        builder.setItems(provinces, new DialogInterface.OnClickListener()
 
        {
 
  
 
           @Override
 
           public void onClick(DialogInterface dialog, int which)
 
          {
 
            /*
 
            * ad变量用final关键字定义,因为在隐式实现的Runnable接口 的run()方法中 需要访问final变量。
 
             */
 
            final AlertDialog ad = new AlertDialog.Builder(
 
           ListDialogTest1.this).setMessage(
 
               "你选择的是:" + which + ": " + provinces[which]).show();
 
            Handler handler = new Handler();
 
            Runnable runnable = new Runnable()
            {
 
              @Override
               public void run()
               {
 
                   // 调用AlertDialog类的dismiss()方法关闭对话框,也可以调用cancel()方法。
 
                   ad.dismiss();
 
               }
 
           };
 
           // 5秒后运行run()方法。
 
           handler.postDelayed(runnable, 5 1000);
 
            }
 
       });
 
        builder.create().show();
 
    }
 
}
原文地址:https://www.cnblogs.com/mochaMM/p/5124177.html