Checking if a button has been clicked

我们两清 提交于 2021-02-10 11:51:28

问题


How to load menu depend on button was clicked? Any idea, solution?

@Override
public boolean onCreateOptionsMenu(final Menu menu) {

    menu.clear();
    if(button.**isclicked**) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
    } else {
        getMenuInflater().inflate(R.menu.test, menu);
    }

    return true;
}

回答1:


Put a boolean field in your activity and change whenever you click button . Depends if its true or false show or not your menu

UPDATE

    //declare boolean
    boolean clicked=false;

    //my button clic
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) 
    {
        //change boolean value
        clicked=true;
    }
    });

    //then on another method or where you want
    if(clicked)
   {
      openmenu();
   }
   else
   {
      closemenu();
   }



回答2:


try this function

button.isPressed();



回答3:


Try something like this:

get the button's id

Button button = (Button) findViewById(R.id.button_send);

Adding click event listener

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //do whatever you want
    }
});



回答4:


THe below also work fine.

if(button.isSelected())


来源:https://stackoverflow.com/questions/30661425/checking-if-a-button-has-been-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!