简体   繁体   English

如果TextView显示特定文本,则将可见性设置为GONE

[英]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. 有什么办法可以让TextView显示特定的文本,例如0%的折扣,使文本可见性设置为其他可见。

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 . 是的,您可以在侦听器中实现您的逻辑,只要用户更改EditText的文本,该逻辑就会触发。 Have your activity implement TextWatcher , then try something like this in your activity: 让您的活动实现TextWatcher ,然后在您的活动中尝试如下操作:

@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 . 注意:此答案最初是在OP询问有关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 ). 从那时起,OP将问题更改为TextView ,但是我上面建议的内容通常可以用于任何侦听器(例如,正在更新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. 如果要显示1%的 CardView则将TextView包裹到CardViewLinearLayout并在recyclerview适配器中尝试此操作。

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);

                    }


                }
            });

    }
}

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

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