【问题标题】:Change background color onclick and then retrieve the original color android更改背景颜色onclick然后检索原始颜色android
【发布时间】:2011-11-24 11:45:51
【问题描述】:

在我的应用程序中,我扩展了基本适配器并创建了自己的适配器。我对行有不同的背景图像取决于它们的位置。我想在单击“完成”时更改任何行的颜色,然后在执行 onclick 后检索第一个背景颜色。这是我的改变背景颜色的 onclick 方法。

    public void onClick(View v) {

            if(pos==0)
            v.setBackgroundResource(R.drawable.stoprow);
            else if(pos==getCount()-1)
            v.setBackgroundResource(R.drawable.sbottomrow);
            else
                v.setBackgroundResource(R.drawable.smiddlerow);

            Intent details = new Intent(v.getContext(),HomeworkDetails.class);
            details.putExtra("date", data.get(pos).getDateOfAdd() );
            details.putExtra("dueDate", data.get(pos).getDuedate());
            details.putExtra("course", data.get(pos).getCourse());
            details.putExtra("note", data.get(pos).getNote());
            details.putExtra("iconLink", links.get(pos));
            v.getContext().startActivity(details);
        }
        });

现在如何更改 onclick() 之后的背景颜色?

【问题讨论】:

  • 我不明白,什么时候你想给视图赋予它的旧背景?您可以在应用新背景之前使用 getBackground() 获取旧背景,并且可以使列表无效()以使您的自定义适配器重绘它。不过我可能不明白这个问题。

标签: android android-listview android-animation


【解决方案1】:

只需使用带有选择器的drawable。用pressed 属性声明两个项目。一真一假。

然后您可以将可绘制对象分配给适配器中的按钮。

带有选择器的可绘制示例:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/button_on" />
<item android:state_pressed="false" android:drawable="@drawable/button_off" /></selector>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2015-02-12
    • 2011-05-23
    相关资源
    最近更新 更多