同一个按钮多次点击

时间:2015-07-27 06:55:40

标签: java android

这个想法是按钮可以在第一次点击时执行一项操作,在第二次点击时执行不同的操作。

button_food   = (Button) findViewById(R.id.foodicon_layout);
button_travel = (Button) findViewById(R.id.travelicon_layout);
button_fuel   = (Button) findViewById(R.id.fuelicon_layout);
button_fetch  = (Button) findViewById(R.id.fetchicon_layout);

button_travel.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        // Perform action on click
        button_food.setVisibility(View.GONE);
        button_fuel.setVisibility(View.GONE);
        button_fetch.setVisibility(View.GONE);
    }
});

在给定的示例中,单击button_travel时,其他按钮将变为不可见。再次点击相同我希望其他按钮再次可见。

5 个答案:

答案 0 :(得分:7)

您可以通过获取当前可见性并切换它来使按钮设置其可见性。

button_food   = (Button) findViewById(R.id.foodicon_layout);
button_travel = (Button) findViewById(R.id.travelicon_layout);
button_fuel   = (Button) findViewById(R.id.fuelicon_layout);
button_fetch  = (Button) findViewById(R.id.fetchicon_layout);

button_travel.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        int visibility = button_food.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE;

        // Perform action on click
        button_food.setVisibility(visibility);
        button_fuel.setVisibility(visibility);
        button_fetch.setVisibility(visibility);
    }
});

这样写它只是编写if语句的简单方法

int visibility;
if(button_food.getVisibility() == View.VISIBLE){
     visibility = View.GONE;
} else {
     visibility = View.VISIBLE;
}

答案 1 :(得分:5)

只需检查当前状态并采取相应措施

public void onClick(View v) {

    // Perform action on click
    if (button_food.getVisibility() == View.VISIBLE) {
        button_food.setVisibility(View.GONE);
    } else {
        button_food.setVisibility(View.VISIBLE
    }

    if (button_fuel.getVisibility() == View.VISIBLE) {
        button_fuel.setVisibility(View.GONE);
    } else {
        button_fuel.setVisibility(View.VISIBLE
    }

    if (button_fetch.getVisibility() == View.VISIBLE) {
        button_fetch.setVisibility(View.GONE);
    } else {
        button_fetch.setVisibility(View.VISIBLE
    }
}

答案 2 :(得分:3)

你在找这个吗?:

public void onClick(View v) {

             if(visible){
                    visible=false;
                    button_food.setVisibility(View.GONE);
                    button_fuel.setVisibility(View.GONE);
                    button_fetch.setVisibility(View.GONE);
             }else{ 
                    visible=true;
                    button_food.setVisibility(View.VISIBLE);
                    button_fuel.setVisibility(View.VISIBLE);
                    button_fetch.setVisibility(View.VISIBLE);
            }
    }

答案 3 :(得分:2)

你可以做的是检查它们的可见性是否可见,设置的可见性去除了将visisiblity设置为Visible以供参考,请看下面的代码

  if (button_food.getVisibility()==View.VISIBLE) {
  button_food.setVisibility(View.GONE);
    } else {
         button_food.setVisibility(View.VISIBLE);
    }

if (button_fuel.getVisibility()==View.VISIBLE) {
  button_fuel.setVisibility(View.GONE);
    } else {
         button_fuel.setVisibility(View.VISIBLE);
    }

 if (button_fetch.getVisibility()==View.VISIBLE) {
         button_fetch.setVisibility(View.GONE);
    } else {
         button_fetch.setVisibility(View.VISIBLE);
    }

答案 4 :(得分:1)

我认为这是最干净的方式:

button_food   = (Button) findViewById(R.id.foodicon_layout);
button_travel = (Button) findViewById(R.id.travelicon_layout);
button_fuel   = (Button) findViewById(R.id.fuelicon_layout);
button_fetch  = (Button) findViewById(R.id.fetchicon_layout);
private boolean visible;

button_travel.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if(visible){
            visible=false;
            button_food.setVisibility(View.GONE);
            button_fuel.setVisibility(View.GONE);
            button_fetch.setVisibility(View.GONE);
        }else{ 
            visible=true;
            button_food.setVisibility(View.VISIBLE);
            button_fuel.setVisibility(View.VISIBLE);
            button_fetch.setVisibility(View.VISIBLE);
        }
    }
});