简体   繁体   中英

select one textview and change its color at the same time out of 4 textviews

I have 4 textviews, out of 4 textviews, I have to select anyone from the 4 textviews, and also I have to change the background color(green) of the textview at the same time to highlight it. Every textview uses custom drawable background.

This is textview xml:

       <TextView
            android:id="@+id/textview1"
            android:layout_width="match_parent"
            android:layout_height="@dimen/_55sdp"
            style="@style/ChoosePlan"
           android:background="@drawable/choose_plan_bg"
            android:text="@string/part_time_monthly"
           />

That means

setBackground(getResources().getDrawable(R.color.green));

will not work.

So I tried like this:

     private void changeColor()
    {
    Drawable tempDrawable = getResources().getDrawable(R.drawable.choose_plan_bg);
    LayerDrawable bubble = (LayerDrawable) tempDrawable; //(cast to root element in xml)
    GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
    solidColor.setColor(getResources().getColor(R.color.green));
    textview1.setBackground(tempDrawable);
    textview1.setTextColor(getResources().getColor(R.color.white));
   }

and onClick Event I call the above method:

   @Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.tv_partTimeMonthly:
            changeColor();
            checkDoneBtnVisibile();
            break;
        case R.id.tv_partTimeAnnually:
            checkDoneBtnVisibile();
            break;
        case R.id.tv_fullTimeMonthly:
            checkDoneBtnVisibile();
            break;
        case R.id.tv_fullTimeAnually:
            checkDoneBtnVisibile();
            break;
        case R.id.iv_back_button:
            finish();
            break;
    }
    }

PS: Have to use Textview(No ImageView)

I got stuck to it. Can you please help me out to move me forward.

Thanks in Advance

Sharing the Screenshot:

在此处输入图片说明

You are only updating textview1 in your changeColour() method. If you just want to highlight 'only' one textView that is pressed at any time, then you may try something like the following.

First, I made a method that takes a textView as its argument that changes its background back to choose_plan_bg . Specify wherever your background is located in drawable.

private void changeColorBack(TextView textView)
{
      textView.setBackground(getResources().getDrawable(R.drawable.choose_plan_bg))
}

Similarly, the changeColour method:

private void changeColor(TextView textView)
{
Drawable tempDrawable = getResources().getDrawable(R.drawable.choose_plan_bg);
LayerDrawable bubble = (LayerDrawable) tempDrawable; //(cast to root element in xml)
GradientDrawable solidColor = (GradientDrawable) bubble.findDrawableByLayerId(R.id.outerRectangle);
solidColor.setColor(getResources().getColor(R.color.green));
textView.setBackground(tempDrawable);
textView.setTextColor(getResources().getColor(R.color.white));


}

Next, create a distinct OnClickListener and set the textViews to this listener:

View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            changeColorBack(textView1);
            changeColorBack(textView2);
            changeColorBack(textView3);
            changeColorBack(textView4);
            switch (v.getId()) {
                case R.id.textView1:
                    changeColor(textView1);
                    break;
                case R.id.textView2:
                    changeColor(textView2);
                    break;
                case R.id.textView3:
                    changeColor(textView3);
                    break;
                case R.id.textView4:
                    changeColor(textView4);
            }

        }
    };

    textView1.setOnClickListener(listener);
    textView2.setOnClickListener(listener);
    textView3.setOnClickListener(listener);
    textView4.setOnClickListener(listener);

This selects only the textView that is pressed and unselects anyone other textView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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