简体   繁体   中英

Set visibility to GONE, if TextView displays a specific text

Is there any way if TextView is showing specific text like 0% off so text visibility set to gone otherwise its visible.

just like this code

if(text.length() == 0 || text.equals(""))
            {
                mTel1.setVisibility(View.GONE);
            } else {
                mTel1.setVisibility(View.VISIBLE);
            }

Yes, you could implement your logic in a listener, which would fire whenever the user changes the text in the EditText . Have your activity implement TextWatcher , then try something like this in your activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    EditText ed = new EditText(this);
    ed.addTextChangedListener(this);
    setContentView(editText);

    // plus your current code
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    String ed_text = ed.getText();
    if (ed_text.length() == 0 || ed_text.equals("")) {
        mTel1.setVisibility(View.GONE);
    }
    else {
        mTel1.setVisibility(View.VISIBLE);
    }
}

Note: This answer was originally given when the OP was asking about an EditText . Since then, the OP changed the question to a TextView , but what I suggest above generally can be used for any listener (eg a listener on whatever is updating the TextView ).

    final TextView text=findViewById(R.id.myTextView);


    text.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {

            if(editable.toString().equals("Your Text ")){

                // 1 - You can set empty text
                text.setText("");
                // 2 - Or you can change the color of the text
                text.setTextColor(Color.TRANSPARENT);
                // 3 - Or you can change the visibility of the view
                text.setVisibility(View.INVISIBLE);


            }else{

                //Here you should undo your code 

                //1 - if you using method one dose not need to do anything here 
                // for method 2 
                text.setTextColor(Color.BLACK);
                // for method 3
                text.setVisibility(View.VISIBLE);
            }


        }
    });

您可以使用更改文字颜色来完成此操作

tvPoints.setTextColor(Color.TRANSPARENT);

if you want to show 1% off wrap the TextView into CardView or LinearLayout and in recyclerview adapter try this.

class ViewHolder extends RecyclerView.ViewHolder{

    public TextView discountedvalue;

    public CardView cardview;

    public ViewHolder(View itemView) {

        super(itemView);
        discountedvalue = (TextView) itemView.findViewById(R.id.DiscountValue);
        cardview = (CardView) itemView.findViewById(R.id.cardview);
        discountedvalue.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {

                    if(editable.toString().equals("0 % off")){

                        cardview.setVisibility(View.GONE);

                    }else{

                        cardview.setVisibility(View.VISIBLE);

                    }


                }
            });

    }
}

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