我将按钮添加到布局文件中。我想知道我的按钮被点击了多少次:

if(btn is clicked == 1) {
     then do this
}
else(btn is clicked > 1) {
  do this
}


我该如何处理?

最佳答案

尝试这种方式,首先在活动类文件上声明全局变量,如下所示:

int clickcount=0;


在将click事件添加到按钮并增加值clickcount变量后,如下代码:

    yourbutton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            clickcount=clickcount+1;
            if(clickcount==1)
            {
                //first time clicked to do this
                Toast.makeText(getApplicationContext(),"Button clicked first time!", Toast.LENGTH_LONG).show();
            }
            else
            {

                //check how many times clicked and so on
                Toast.makeText(getApplicationContext(),"Button clicked count is"+clickcount, Toast.LENGTH_LONG).show();
            }
        }
    });

10-05 17:48