1
在我的android应用程序中,我改变按钮图像1秒。有一组按钮。在任何一个按钮的1秒图像被改变。如果在1秒我点击那个特定的按钮我想执行一些operation.using的onclick监听我tried.but它不是我working.How能做到这一点?请帮我 下面是代码 -onclick android的按钮聆听者
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
// **here i want to call method for onclick of button.**
// buttonclick();
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
if(time==-1){
onStop();
}
else
runOnUiThread(new Runnable() {
public void run() {
Random rand=new Random();
time=time-1;
but1=(Button) findViewById(R.id.b1);
but1.setBackgroundResource(R.drawable.happy);
but1.setContentDescription("happy");
but2=(Button) findViewById(R.id.b2);
but2.setBackgroundResource(R.drawable.happy);
but2.setContentDescription("happy");
int num = rand.nextInt(buttonIds.length);
int buttonId = buttonIds[num];
Button bb=(Button) findViewById(buttonId);
if(bb.getContentDescription().equals(button.getContentDescription()))
{
bb.setBackgroundResource(R.drawable.happy);
bb.setContentDescription("happy");
wrong++;
}
else
{
bb.setBackgroundResource(R.drawable.whoa);
count++;
bb.setContentDescription("whoa");
}
}
});
}
},0, 1000);
}
public void onClick(View v){
//when i clicked on any button its not even entering here
System.out.println("in onlcik............");
int aaa=v.getId();
System.out.println("click id is------------"+aaa);
for(i=0;i<9;i++){
if(aaa==buttonIds[i]){
findViewById(buttonIds[i]).setOnClickListener(new View.OnClickListener() {
Drawable dd=findViewById(buttonIds[i]).getBackground();
public void onClick(View arg0) {
System.out.println("yes...");
}
});
}
}
}
}
我更新了代码 - 我用onclicklistener.but它也不工作。
我有10个buttons.i要调用一个函数,我想按钮的ID被点击.like view.getId();我怎么能写这个功能。 – 2013-02-11 04:30:45
我编辑了代码 – 2013-02-11 04:37:26
只需定义一个变量,例如''lastButtonId'并将其设置为'button.getId();'当您在buttonclick()函数中处理按钮onClick()' – iTech 2013-02-11 04:40:34