Android-以编程方式获取listview(item)的背景色 [英] Android-Get Background color of listview(item) programmatically

查看:73
本文介绍了Android-以编程方式获取listview(item)的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我为列表视图项创建背景颜色,如下所示,我有两个这样的自定义列表视图:

First I create a background color for listview items like below, I have two custom listview like this:

我的自定义颜色设置列表:

My custom list for setting color:

代码:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        final View rowView;
        rowView = inflater.inflate(R.layout.table_one, null);
        holder.tv = (TextView) rowView.findViewById(R.id.textView1);
        holder.tv.setText(result[position]);
        rowView.setBackgroundColor(Color.WHITE);
        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (position == 0) {
                    rowView.setBackgroundColor(Color.BLUE);
                    MainActivity.counter = Color.BLUE;
                } else if (position == 1) {
                    rowView.setBackgroundColor(Color.YELLOW);
                    MainActivity.counter = Color.YELLOW;
                } else if (position == 2) {
                    rowView.setBackgroundColor(Color.GREEN);
                    MainActivity.counter = Color.GREEN;
                } else if (position == 3) {
                    rowView.setBackgroundColor(Color.MAGENTA);
                    MainActivity.counter = Color.MAGENTA;
                }
            }
        });
        return rowView;
    }

当用户单击列表视图时,它将变为指定的颜色,如上图所示,

When user click on listview it changes to specifed color as above image,

当我单击按钮时,我需要获取保存在列表中的listview的颜色.

i need to get the color of listview saved in the list when click teh button.

我该怎么办,需要帮助

在MainActivity中,我有一个按钮,

In my MainActivity I have button ,

check = (Button) findViewById(R.id.check);
 check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(getApplicationContext(), "Checked", Toast.LENGTH_SHORT).show();
            }
        });

推荐答案

如果要从ListView获取视图背景色

If you want to obtain a view background colour, from a ListView

ListView listView = (ListView) findViewById(R.id.my_list);

获得背景尝试

Drawable listDrawableBackground = listView.getBackground();

如果背景是一种颜色,要获得该颜色,您可以尝试

if background is a colour, to obtain the colour, you can try

ColorDrawable listViewColor = (ColorDrawable) listView.getBackground();

要获取实际颜色,请使用

to get the actual colour, use

int colorId = listViewColor.getColor();

您可以将此colorId与现有颜色进行比较,如下所示:

you can compare this colorId with existing color like so:

if(colorID == R.color.blue){Log(颜色为蓝色");}

另一种方法是将标签添加到ListView,标签将是颜色,例如

Another way will be to add a tag to the ListView, and the tag will be the color, say

tag ="#ccc",您可以使用

tag = "#ccc", and you can use

String colorCode = (String)listView.getTag();

现在您有了colorCode,并且可以在程序中的任何地方重复使用它.

now you have the colorCode and you can reuse it anywhere in your program.

这篇关于Android-以编程方式获取listview(item)的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆