Android按钮2点击
问题描述:
如何在点击2次后更改android按钮? 第一时间更改按钮,我会压制后再次使用此代码Android按钮2点击
{
public void onClick(View v) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
}
}
我想更改的按钮来查看一遍 我该怎么办呢?
答
也许不喜欢这样写道:
int count = 0;
public void onClick(View v) {
count++;
if(count == 2){
count = 0;
b.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.menubuttonpressed));
}
}
这将在您的按钮(视图)每2点击后成立的背景。
答
private int clickCount =0;
public void onClick(View v) {
if (clickCount==0) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
} else {
// do something else
}
clickCount++;
}
答
那么,一种方法是保持一个计数器。
numberOfClicks = 0;
...
public void onClick(View v) {
...
if(numberOfClicks==0)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
else if(numberofClicks==1)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
...
numberofClicks++;
}
取一个计数器变量。每当按钮被点击时每增加一次。检查条件并设置图像。 – 2012-08-01 10:50:27
保留一个全球计数器,而不是跟踪点击次数。然后执行'if(clickCount> 1)..Changebutton image' – Doomsknight 2012-08-01 10:50:31