簡體   English   中英

如果TextView顯示特定文本,則將可見性設置為GONE

[英]Set visibility to GONE, if TextView displays a specific text

有什么辦法可以讓TextView顯示特定的文本,例如0%的折扣,使文本可見性設置為其他可見。

就像這段代碼

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

是的,您可以在偵聽器中實現您的邏輯,只要用戶更改EditText的文本,該邏輯就會觸發。 讓您的活動實現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);
    }
}

注意:此答案最初是在OP詢問有關EditText 從那時起,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);

如果要顯示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