android 简单列表对话框(AlertDialog.Builder().setItems())
fmms
13年前
<img alt="android 简单列表对话框(AlertDialog.Builder().setItems()) " src="https://simg.open-open.com/show/d6c963d505cef78f9d7ca1a12054dab7.jpg" width="331" height="115" /> <div> <img alt="android 简单列表对话框(AlertDialog.Builder().setItems()) " src="https://simg.open-open.com/show/d7cafdd926883ff55a4b075972f8e0d4.jpg" width="295" height="381" /> <br /> <br /> </div> <br /> <div> <img alt="android 简单列表对话框(AlertDialog.Builder().setItems()) " src="https://simg.open-open.com/show/417e87416c7726f2bbf2c8b84bbc6d89.jpg" width="267" height="54" /> <br /> <br /> <p><a name="OLE_LINK3" rel="nofollow"></a> </p> <pre class="brush:java; toolbar: true; auto-links: false;">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(); } }</pre> <p></p> </div>