Android中的onclick监听器按钮

时间:2013-02-11 04:20:54

标签: android button view onclick

在我的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。但它也无效。

1 个答案:

答案 0 :(得分:0)

假设您已为按钮正确设置onClick处理程序,只需拨打button.performClick()

即可调用它
相关问题