根据if语句更改视图的背景颜色

时间:2018-11-29 16:21:58

标签: android view

我的问题是关于尝试在代码中的多个位置更改Android Studio中视图的背景颜色。

我正在开发一个预订系统,并尝试根据是否预订了太多单一资源来更改视图的颜色。 这个想法是调用一个名为“ tooManyItems()”的函数来进行检查。并相应地更改颜色以及其他一些更改(但这些更改在下面的代码中有效)

我正在使用API​​ 20,以确保更多设备上的功能。

为了理解:

  • input是具有自定义类Ressource的ArrayList。这个班有一个int 变量称为组。我们进行检查,因为如果group == 1,则视图没有 编辑文本窗口

主要问题:

如果在tooManyItems的if和else的最内层循环中都没有,则无法使setBackgroundColor可以在main或info上使用。简而言之:它将改变颜色一次。但不来回。 重要的是不要使循环中的前两行代码始终有效。我可以让textview改变背景,而imgError看起来没问题。

我这样宣告我的观点:

        for (int i = 0; i < input.size(); i++) {
           nRessourcer.add((LinearLayout) inflater.inflate(R.layout.valgt_ressource, null));
           editText_Amount.add((EditText) nRessourcer.get(i).findViewById(R.id.editText_Antal));
           imgError.add((ImageView) nRessourcer.get(i).findViewById(R.id.imgError));
           imgDelete.add((ImageView) nRessourcer.get(i).findViewById(R.id.imgDelete));
           textView_Ressource.add((TextView) nRessourcer.get(i).findViewById(R.id.textView_Ressource));
    }
点击视图时会声明

info,我们将调用expand()函数,如下所示:

    public void expand() {
        if (isExpanded) {
//main.setBackgroundColor(colorItemAvailable);
            textView_header.setTextColor(colorWhite);
            textView_subheader.setTextColor(colorWhite);
            textView_pakkeTotal.setTextColor(colorWhite);
            expander.removeView(info);
            if (isBooked) expander.addView(reserveret);

            isExpanded = false;
        } else {
//                main.setBackgroundColor(test);
            textView_header.setTextColor(colorBlack);
            textView_subheader.setTextColor(colorBlack);
            textView_pakkeTotal.setTextColor(colorBlack);
            expander.addView(info);

            if (isBooked) expander.removeView(reserveret);
            isExpanded = true;
        }
}

此后,我调用tooManyItems函数。

public void tooManyItems() {

    for (int i = 0; i < nRessourcer.size(); i++) {
        editText_Amount.get(i).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                for (int i = 0; i < nRessourcer.size(); i++) {
                    if (input.get(i).getGroup() == 2) {
                        if (!editText_Amount.get(i).getText().toString().matches("")) {
                            if (input.get(i).getAvaliable() < Integer.parseInt(editText_Amount.get(i).getText().toString())) {
                                imgError.get(i).setVisibility(View.VISIBLE);
                                main.setBackgroundColor(colorYellow);
                                infoMain.setBackgroundColor(colorYellow);
                                buttonReserver.setBackgroundColor(colorItemUnavailable); 
                                buttonReserver.setEnabled(false);  


                            } else {
                                if (input.get(i).getLedige() >= Integer.parseInt(editText_Antal.get(i).getText().toString())) {imgError.get(i).setVisibility(View.INVISIBLE);
                                    editText_Amount.get(i).setBackgroundColor(colorGrey);
                                    main.setBackgroundColor(colorItemSelected);
                                    infoMain.setBackgroundColor(colorItemSelected);
                                    buttonReserver.setBackgroundColor(colorBlue);
                                    buttonReserver.setEnabled(true);
                                }
                            }
                        }
                    }
                }
            }

如果您需要其他代码,请告诉我。我认为不需要提供整个课程,因为其他所有内容都可以正常工作,而导致问题的只是相互作用。在添加tooManyItems之前,Expand也可以自行开发。

希望您能提供帮助!

1 个答案:

答案 0 :(得分:0)

通过尝试从头开始重写它,我找到了修复程序!

在toManyItems的开头,我删除了for循环,并为其提供了最终输入,内部函数可以使用该输入,以便启动看起来像这样:

public void tooManyItems(final int i) {

    editText_Amount.get(i).addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }
and so on...

相反,我使用for循环调用该函数,如下所示:

    for (int i = 0; i < nRessourcer.size(); i++) {
        tooManyItems(i);
    }

它现在会像预期的那样响应。

相关问题