在我的 android 应用程序中,我将按钮图像更改 1 秒。有一组按钮。在这 1 秒内,任何一个按钮的图像都会被更改。如果在那 1 秒内我单击该特定按钮,我想执行一些操作。使用我尝试过的 onclick 侦听器。但它不起作用。我该怎么做?请帮助我下面是代码 -
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 它也不起作用。