Android 根据单击的列表项更改应用程序的背景颜色

Android 根据单击的列表项更改应用程序的背景颜色,android,colors,onclick,Android,Colors,Onclick,我有一个带有颜色名称的列表视图,字符串数组存储在单独的xml中。如何根据单击列表中的颜色更改应用程序的背景?我有一个函数,根据单击的项目显示toast消息,但我不知道如何将其转换为背景颜色更改函数 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView parent, View view, int position, long id)

我有一个带有颜色名称的列表视图,字符串数组存储在单独的xml中。如何根据单击列表中的颜色更改应用程序的背景?我有一个函数,根据单击的项目显示toast消息,但我不知道如何将其转换为背景颜色更改函数

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View view, int position, long id) {

            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

        }
    });

您的活动有一个布局

给它起个名字这里有一个叫外容器

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/outer_container"
            android:focusable="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

在监听器中,您可以根据ListView项的文本更改颜色

要解决此问题,只需使用if-else检查TextView的值,然后更改颜色。我假设您没有颜色资源,所以我使用ARGB值

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        //Save the color name into a variable
        String colorName=((TextView) view).getText().toString();

        //Default is color White
        int color=Color.argb(255, 255, 255, 255);

        //Check the color name
        if(colorName.equals("black"))
        {
           color=Color.argb(255, 0, 0, 0);
        }
        else if (colorName.equals("red"))
        {
            color=Color.argb(255, 255, 0, 0);
        }
        //...and so on with other colors

        //Find the background view using the layout id. Then you will be able to change the background with the color
        findViewById(R.id.id_of_the_layout).setBackgroundColor(color);

    }
});

ListView有一个适配器、一个自定义适配器或一个类似ArrayAdapter的标准适配器。比如:

ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
因此,在您的itemClickListener中:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        String colorCode = itemsAdapter.getItem(position);
        setBackgroundColor(colorCode);
    }
});
set backgroundColor方法将使用对启动时必须存储的父容器的引用

View parentContainer;

...
// at onCreate
parentContainer = findViewById(R.id.container_id);
...

void setBackgroundColor(String colorCode) {
    int color = Color.parseColor(colorCode);
    parentContainer.setBackgroundColor(color);
}

对我来说,问题的可能重复是从ListView获取值,然后将背景色设置为该值。实际上,我有两个数组,一个带有颜色名称,另一个带有十六进制值。如何将它们放在一起?如果它们以相同的顺序存储,例如,Array1=[Black,Red…]和Array2=[000000,FF0000…],那么您需要这样做:color=color.parseColorArray2[position],因此使用这种方式,即使您不需要If-else
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        String colorCode = itemsAdapter.getItem(position);
        setBackgroundColor(colorCode);
    }
});
View parentContainer;

...
// at onCreate
parentContainer = findViewById(R.id.container_id);
...

void setBackgroundColor(String colorCode) {
    int color = Color.parseColor(colorCode);
    parentContainer.setBackgroundColor(color);
}