Android中AlertDilog显示简单和复杂列表的方法
本文实例讲述了Android中AlertDialog显示简单和复杂列表的方法。分享给大家供大家参考,具体如下:
AlertDialog 显示简单列表 setItems
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 ListDialogTest1extends Activityimplements 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(); } }
AlertDialog 显示复杂列表 setMultiChoiceItems
一、通过AlertDialog.Builder类的setMultiChoiceItems()方法可以创建列表对话框,方法如下:
1、 从资源文件中装载数据:
public Builder setMultiChoiceItems(int itemsId, boolean[]checkedItems, final OnMultiChoiceListener listener)
2、从数据集中装载数据
public Builder setMultiChoiceItems(Cursor cursor, StringisCheckedColumn, String labelColumn, final OnMultiChoiceListenerlistener)
3、从字符串数组中装载数据
public Builder setMultiChoiceItems(CharSequence[] items,boolean[] checkedItems, final OnMultiChoiceListener listener)
import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.content.DialogInterface.OnMultiChoiceClickListener; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class DiaActivity extends Activity { /** Called when the activity is first created. */ private Button btn; private TextView tv; private String[] shuxing = { "字体颜色", "字体大小" }; private boolean[] chuzhi = { false, false }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.button1); tv = (TextView) findViewById(R.id.textView1); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Builder b = new AlertDialog.Builder(DiaActivity.this); b.setTitle("gggggg"); b.setMultiChoiceItems(shuxing, chuzhi, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { switch (which) { case 0: chuzhi[0] = isChecked; break; case 1: chuzhi[1] = isChecked; break; default: break; } } }); b.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (chuzhi[0] == true) { tv.setTextColor(Color.BLUE); } if (chuzhi[1] == true) { tv.setTextSize(50f); } } }); b.create().show(); } }); } }
二、通过AlertDialog.Builder类的setSingleChoiceItems方法可以创建带单按钮的列表:方法如下:
1、从资源文件中装载数据:
public Builder setSingleChoiceItems(int itemsId, intcheckedItem, final OnClickListener listener)
2、从数据集中装载数据
public Builder setSingleChoiceItems(Cursor cursor, intcheckedItem, String labelColumn, final OnClickListenerlistener)
3、从字符串数组中装载数据
public Builder setSingleChoiceItems(CharSequence[] items, intcheckedItem, final OnClickListener listener)
4、从ListAdapter对象中装载数据
public Builder setSingleChoiceItems(ListAdapter adpater, intcheckedItem, final OnClickListener listener)
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; import android.widget.Toast; public class SingleChoiceItemsTest extends Activityimplements OnClickListener { private String[] province = new String[] { "上海", "北京", "湖南", "湖北", "海南" }; private Button btnSingleChoiceList; // 单击事件对象的实例 private ButtonOnClick buttonOnClick = new ButtonOnClick(1); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnSingleChoiceList = (Button) findViewById(R.id.btnSingleChoice); btnSingleChoiceList.setOnClickListener(this); } @Override public void onClick(View v) { showSingleChoiceButton(); } // 在单选选项中显示 确定和取消按钮 //buttonOnClickg变量的数据类型是ButtonOnClick,一个单击事件类 private void showSingleChoiceButton() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择省份"); builder.setSingleChoiceItems(province, 0, buttonOnClick); builder.setPositiveButton("确定", buttonOnClick); builder.setNegativeButton("取消", buttonOnClick); builder.show(); } private class ButtonOnClick implements DialogInterface.OnClickListener { private int index; // 表示选项的索引 public ButtonOnClick(int index) { this.index = index; } @Override public void onClick(DialogInterface dialog,int which) { // which表示单击的按钮索引,所有的选项索引都是大于0,按钮索引都是小于0的。 if (which >= 0) { //如果单击的是列表项,将当前列表项的索引保存在index中。 //如果想单击列表项后关闭对话框,可在此处调用dialog.cancel() //或是用dialog.dismiss()方法。 index = which; } else { //用户单击的是【确定】按钮 if (which == DialogInterface.BUTTON_POSITIVE) { //显示用户选择的是第几个列表项。 final AlertDialog ad = new AlertDialog.Builder( SingleChoiceItemsTest.this).setMessage( "你选择的地区是:" + index + ":" + province[index]).show(); //五秒钟后自动关闭。 Handler hander = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { ad.dismiss(); } }; hander.postDelayed(runnable, 5 * 1000); } //用户单击的是【取消】按钮 else if (which == DialogInterface.BUTTON_NEGATIVE) { Toast.makeText(SingleChoiceItemsTest.this, "你没有选择任何东西", Toast.LENGTH_LONG); } } } } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。