Android 自定义列表对话框

Android custom list dialog(Android 自定义列表对话框)
本文介绍了Android 自定义列表对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述


我正在开发一个简单的文件浏览器应用程序.我有大部分设置(它在不同的目录中列出了一切,而且没有),但我现在被困在一起(在几个小时内工作了几个小时)是选择列表项目时,我想要出现一个自定义列表对话框.我在android开发页面上找到了这段代码并稍微修改了一下.目前它只是对所选内容进行了祝酒,但我需要将这三个项目分开.也就是说,我想做的不仅仅是祝酒,让每个选择运行不同的命令.这是我当前的代码

Hi,
I'm working on a simple file browser app. I have most of it set up (where it lists everything out in the different directories and what not) but what I'm stuck on right now (worked on it for a few hours) is when a list item is selected, I want to have a custom list dialog appear. I found this code on the android development page and modded it slightly. Currently it just gives a toast of what was selected but I need the three items to be separate. That is, I'd like to do more than a toast and have each selection run different commands. Here is my current code

    final CharSequence[] items = {"Info", "Rename", "Delete"};

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Options for " + file.getName());
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
        }
    }).show();

感谢任何可以帮助我将其分开的人.我尝试了几种不同的 if 语句变体,但我尝试过的一切都失败了.

Thanks to anyone who can help me just separate it. I've tried a few different variations of if statements and what not but everything I've tried has failed.

推荐答案

您收到的项目整数是包含您的操作的字符序列数组的索引,因此要获取选择的操作,您可以这样做(在您的onClick 方法):

The item integer you receive is the index of the charsequence array that contains your actions, so to get the action that was selected you could do like this (inside your onClick method):

if (item == 0)
{
     // Info item
}
else if (item == 1)
{
     // Rename, and so one

或者你可以这样做:

if (items[item].equals("Info"))
{
     // Info item
}
else if (items[item].equals("Rename")
{
     // Rename, and so one
}

但首选第一种方法

这篇关于Android 自定义列表对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How To Create a Rotating Wheel Control?(如何创建转轮控件?)
How to avoid restarting activity when orientation changes on Android(如何在 Android 上的方向更改时避免重新启动活动)
Screen orientation lock(屏幕方向锁定)
Strange behavior with android orientation sensor(android方向传感器的奇怪行为)
Android: Rotate image in imageview by an angle(Android:将imageview中的图像旋转一个角度)
Activity restart on rotation Android(旋转Android上的活动重启)