以编程方式更改Android AlertDialog中的单个项目背景颜色

时间:2016-11-14 12:01:03

标签: android background-color

我试图在我的Android应用中快速选择特定颜色,并且我希望能够在简单对话框中将列表选项设置为特定颜色。似乎应该很容易,但我还没有找到直接的答案。

我如何(在运行时以编程方式)相应地设置每个选项的背景颜色?

2 个答案:

答案 0 :(得分:0)

我认为这可能有用:

private void showAlertDialogForColors() {
        final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LayoutInflater inflater = this.getLayoutInflater();
        View alertLayout = inflater.inflate(R.layout.choose_color_theme, null);
        //alert.setTitle("Theme Color");
        // this is set the view from XML inside AlertDialog
        alert.setView(alertLayout);
        final Integer tempValue = SessionManager.getInstance(ColorSettingsActivity.this).getPrefThemeCode();

        TextView textViewBlue = (TextView) alertLayout.findViewById(R.id.themeColorBlue);
        TextView textViewOrange = (TextView) alertLayout.findViewById(R.id.themeColorOrange);
        TextView textViewPink = (TextView) alertLayout.findViewById(R.id.themeColorPink);
        TextView textViewPurple = (TextView) alertLayout.findViewById(R.id.themeColorPurple);
        TextView textViewCyan = (TextView) alertLayout.findViewById(R.id.themeColorCyan);
        TextView textViewDarkGreen = (TextView) alertLayout.findViewById(R.id.themeColorDarkGreen);
        selectedThemeColor = (TextView) alertLayout.findViewById(R.id.textViewSelectedTheme);
        selectedThemeColor.setTypeface(font);

        textViewBlue.setOnClickListener(this);
        textViewOrange.setOnClickListener(this);
        textViewPink.setOnClickListener(this);
        textViewPurple.setOnClickListener(this);
        textViewCyan.setOnClickListener(this);
        textViewDarkGreen.setOnClickListener(this);


        // disallow cancel of AlertDialog on click of back button and outside touch
        alert.setCancelable(false);
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SessionManager.getInstance(MessageSettingsActivity.this).setPrefThemeCode(tempValue);
                Toast.makeText(getBaseContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
            }
        });
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                recreate();
            }
        });
        AlertDialog dialog = alert.create();
        dialog.show();
    }

如果您有一组预定义的颜色,请使用此颜色,或者您希望传递动态存储在Shared PreferencesDB中的颜色并填充在列表中

然后在我的情况下点击特定项目时,实施开关来执行操作,有5个常数。

答案 1 :(得分:0)

感谢您的回复,伙计们。我已经弄清楚了......

private void showColourChooserDialog(){
    final ColourItem[] items = {
            new ColourItem(Color.RED),
            new ColourItem(Color.GREEN),
            new ColourItem(Color.BLUE),
    };      

    ListAdapter adapter = new ArrayAdapter<ColourItem>(
            this,
            android.R.layout.select_dialog_item,
            android.R.id.text1,
            items){
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            TextView tv = (TextView)v.findViewById(android.R.id.text1);

            int colour = items[position].getColour();
            tv.setBackgroundColor(colour);

            return v;
        }
    };

    new AlertDialog.Builder(this)
    .setTitle("Choose Colour")
    .setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // ... respond to choice here
        }
    }).show();
}

ColourItem类:

public class ColourItem {
    private String displayString;
    private int colour;

    public ColourItem (int colour) {
        this(colour, "");
    }
    public ColourItem (int colour, String displayString) {
        this.colour = colour;
        this.displayString = displayString;
    }

    @Override
    public String toString() {
        return displayString;
    }
    public void setDisplayString(String s){
        this.displayString = s;
    }

    public int getColour(){
        return colour;
    }
    public void setColour(int i){
        this.colour = i;
    }
}

答案的灵感来自:This solution regarding custom icons for dialog items