How to set a view to "invisible" or "gone" when anything other then that view is clicked on

huangapple 未分类评论46阅读模式
英文:

How to set a view to "invisible" or "gone" when anything other then that view is clicked on

问题

我正在尝试构建一个应用程序,我有一个按钮,当点击它时,会将一个线性布局更改为“VISIBLE”。目前将其改回为“GONE”的唯一方法是再次点击相同的按钮,我希望当点击除该线性布局以外的任何内容时,它会变为“GONE”。

以下是设置线性布局为“VISIBLE”或“GONE”的当前代码:

box_seekbar=findViewById(R.id.box_seekbar);
box_price=findViewById(R.id.box_price);
box_price.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (box_seekbar.getVisibility()==GONE){
            box_seekbar.setVisibility(VISIBLE);
        } else {
            box_seekbar.setVisibility(GONE);
        }
    }
});
英文:

i have an app im trying to build and i have a button that when clicked turns a linearlayout to "VISIBLE",currently the only way to turn it back to "GONE" is if i click on the same button again, i would like it so it changes to "GONE" when anything other then that linearlayout is clicked on.

Here's my current code for setting the linearlayout as "VISIBLE" or "GONE":

box_seekbar=findViewById(R.id.box_seekbar);
box_price=findViewById(R.id.box_price);
box_price.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (box_seekbar.getVisibility()==GONE){
           box_seekbar.setVisibility(VISIBLE);
           }
           else{
                box_seekbar.setVisibility(GONE);
           }

           }
});

答案1

得分: 0

boolean check = false;

box_seekbar = findViewById(R.id.box_seekbar);
box_price = findViewById(R.id.box_price);
box_price.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (!check) {
            box_seekbar.setVisibility(VISIBLE);
            check = true;
        } else {
            box_seekbar.setVisibility(GONE);
            check = false;
        }
    }
});
英文:
boolean check = false ; 

box_seekbar=findViewById(R.id.box_seekbar);
box_price=findViewById(R.id.box_price);
box_price.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!check){
           box_seekbar.setVisibility(VISIBLE);
          check = true ; 
           }
           else{
                box_seekbar.setVisibility(GONE);
           check = false ; 
           }

           }
});

huangapple
  • 本文由 发表于 2020年4月3日 19:23:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/61010775.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定