简体   繁体   English

选中时更改按钮的颜色

[英]change the color of button when selected

I am making an app with 3 buttons. 我正在制作一个带有3个按钮的应用程序。

I have the following .xml file that has rectangular button. 我有具有矩形按钮的以下.xml文件。

Now I want to to make them so if one is selected then that button turns red while the other two remain green. 现在,我要制作它们,以便如果选择了一个,则该按钮变为红色,而另两个保持绿色。

On forward action it was working properly: if i click c button and come back and click A button both are showing same red color and if i click B and after A button then also showing same color after some delay it is appearing . 在前进操作中,它工作正常:如果我单击c按钮,然后再次单击并单击A按钮,两者都显示相同的红色,如果我单击B,并且在A按钮后单击,然后经过一段时间的延迟后也显示相同的颜色,则它会出现。

How can I solve this? 我该如何解决?

XML: XML:

  <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <corners android:radius="3dp" />
      <solid android:color="#124a01" />

    <stroke
        android:width="2px"
        android:color="#c8ea32" />

</shape>

Activity: 活动:

public void onClick(View v) {       
                    DeselectButtons();
                    EnabledButton = dynamicTextView.getId();
                    clickedid=dynamicTextView.getId();

                    dynamicTextView.setBackgroundColor(Color
                            .parseColor("#cf0000"));
                    dynamicTextView.setSelected(true);
                    invoiceToDisplay = null;
                    invoiceToDisplay = new ArrayList<String>();
                    text = dynamicTextView.getText().toString();
                    if(text.contains("Bill Numbers"))
                    {
                        text=text.replace("Bill Numbers","");
                    }
                    String s[] = text.split("  , ");

                    invoice = text.split("  , ");
                    System.out.println("s" + s[0]);
                    istouched=true;
                    refreshlist=1;
                    if (s.length == 1) {

                        if(s[0].contains("\n"))
                        {
                            s[0]=s[0].replace("\n","");
                        }
                        int invoice11=receiptlist.indexOf(s[0].trim());
                        String invoiceselected=invoiceList.get(invoice11);

                        tv1.setVisibility(View.GONE);
                        tv2.setVisibility(View.GONE);
                        tv.setVisibility(View.VISIBLE);
                        footerText3.setVisibility(View.GONE);
                        footerText2.setVisibility(View.GONE);
                        loadListViews(invoiceselected, listView1, headerButton1);
                        invoice1 = invoiceselected;
                        invoice2 = "";
                        invoice3 = "";
                        headerButton2.setText("");
                        headerButton3.setText("");
                        adapter = new CustomAdapter(
                                PendingOrdersActitvity.this, itemsList2);

                        listView2.setAdapter(adapter);
                        adapter = new CustomAdapter(
                                PendingOrdersActitvity.this, itemsList2);
                        //adapter = new CustomAdapter(getBaseContext(),itemsList2,PendingOrdersActitvity.this);
                        listView3.setAdapter(adapter);


                        invnumber = "";
                    } else if (s.length == 2) {
                        if(s[0].contains("\n"))
                        {
                            s[0]=s[0].replace("\n","");
                        }

                        int invoice11=receiptlist.indexOf(s[0].trim());
                        String invoiceselected=invoiceList.get(invoice11);

                        int invoice12=receiptlist.indexOf(s[1].trim());
                        String invoiceselected1=invoiceList.get(invoice12);


                        invoice1 = invoiceselected;
                        invoice2 = invoiceselected1;
                        invoice3 = "";
                        loadListViews(invoiceselected, listView1, headerButton1);
                        loadListViews(invoiceselected1, listView2, headerButton2);
                        adapter = new CustomAdapter(
                                PendingOrdersActitvity.this, itemsList2);

                        listView3.setAdapter(adapter);
                        headerButton3.setText(""); footerText3.setVisibility(View.GONE); tv2.setVisibility(View.GONE);

                        invnumber = "";
                    } else if (s.length == 3) {

                        if(s[0].contains("\n"))
                        {
                            s[0]=s[0].replace("\n","");
                        }
                        int invoice11=receiptlist.indexOf(s[0].trim());
                        String invoiceselected=invoiceList.get(invoice11);

                        int invoice12=receiptlist.indexOf(s[1].trim());
                        String invoiceselected1=invoiceList.get(invoice12);

                        int invoice13=receiptlist.indexOf(s[2].trim());
                        String invoiceselected2=invoiceList.get(invoice13);



                        tv.setVisibility(View.VISIBLE);
                        tv1.setVisibility(View.VISIBLE);
                        tv2.setVisibility(View.VISIBLE);

                        invoice1 = invoiceselected;
                        invoice2 = invoiceselected1;
                        invoice3 = invoiceselected2;
                        loadListViews(invoiceselected, listView1, headerButton1);
                        loadListViews(invoiceselected1, listView2, headerButton2);
                        loadListViews(invoiceselected2, listView3, headerButton3);
                        invnumber = "";
                    } //thread.start();
                }
            });
            Button button = new Button(this);
            button.setLayoutParams(new LayoutParams(10,
                    LayoutParams.MATCH_PARENT));
            linear.addView(dynamicTextView);
            linear.addView(button);
            count = 0;
            invoiceNumber = "";
            // invoice=null;
            billnumber = "";            
          }         
     }  
}

Method: 方法:

public void DeselectButtons() {
        for (int i = 1; i < id; i++) {

            if (EnabledButton != i) {
                if (this.findViewById(i) == null) {

                } else {

                    this.findViewById(i).setBackgroundColor(
                            Color.parseColor("#1c7900"));
                }           
                } else {
                this.findViewById(i).setBackgroundColor(
                        Color.parseColor("#1c7900"));           
                }       
        }

    }

Take three RadioButtons and put them inside a RadioGroup and make them look like a button by setting a selector as background. 取三个RadioButtons并将其放在RadioGroup ,通过将选择器设置为背景使它们看起来像一个按钮。 So at a time only one will be selected and get highlighted. 因此,一次只能选择一个并使其突出显示。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
    <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>

This way you will not need to handle the change and color manually. 这样,您将无需手动处理更改和颜色。

Initially keep all the buttons green.Keep track of previous clicked button. 最初使所有按钮保持绿色。跟踪先前单击的按钮。

int previousbutton = 0;

then in onclick: 然后在onclick中:

public void onClick(View v){
    if(previousbutton != 0){
        findViewById(previousbutton).setBackgroundColor(//set green color);
    }
        v.setBackgroundColor(//set red color);
        previousbutton = v.getId();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM