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

时间:2018-06-12 06:01:27

标签: java android textview

如果TextView显示特定文字,例如 0%关闭,那么是否有任何方法可以将文字可见性设置为其他方式可见。

就像这段代码一样

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

4 个答案:

答案 0 :(得分:1)

是的,您可以在侦听器中实现您的逻辑,只要用户更改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的任何监听器)。

答案 1 :(得分:1)

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


        }
    });

答案 2 :(得分:0)

您可以使用更改文字颜色

完成此操作
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.js"></script>


<script>
$(document).ready(function(){
        $('#mustfocus').focus();
});
</script>

</head>
<body>
<input type="button" id="myAnchor" value="Get focus">
<input type="text" id="mustfocus"/>


</body>
</html>

答案 3 :(得分:0)

如果你想显示 1%off TextView打包到CardViewLinearLayout,请在recyclelerview适配器中尝试此操作。

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

                    }


                }
            });

    }
}