如果 TextView 显示特定文本,则将可见性设置为 GONE
Set visibility to GONE, if TextView displays a specific text
如果 TextView
显示特定文本,如 0% off,有什么办法可以将文本可见性设置为消失,否则可见。
就像这个代码
if(text.length() == 0 || text.equals(""))
{
mTel1.setVisibility(View.GONE);
} else {
mTel1.setVisibility(View.VISIBLE);
}
您可以使用更改文本颜色来完成此操作
tvPoints.setTextColor(Color.TRANSPARENT);
是的,您可以在侦听器中实现您的逻辑,只要用户更改 EditText
中的文本,它就会触发。让你的 activity 实现 TextWatcher
,然后在你的 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);
}
}
注意:这个答案最初是在 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);
}
}
});
如果你想显示 1% off 将 TextView
包装到 CardView
或 LinearLayout
并在 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);
}
}
});
}
}
如果 TextView
显示特定文本,如 0% off,有什么办法可以将文本可见性设置为消失,否则可见。
就像这个代码
if(text.length() == 0 || text.equals(""))
{
mTel1.setVisibility(View.GONE);
} else {
mTel1.setVisibility(View.VISIBLE);
}
您可以使用更改文本颜色来完成此操作
tvPoints.setTextColor(Color.TRANSPARENT);
是的,您可以在侦听器中实现您的逻辑,只要用户更改 EditText
中的文本,它就会触发。让你的 activity 实现 TextWatcher
,然后在你的 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);
}
}
注意:这个答案最初是在 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);
}
}
});
如果你想显示 1% off 将 TextView
包装到 CardView
或 LinearLayout
并在 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);
}
}
});
}
}