android button setOnClickListener

648 ワード

Button button = (Button) findViewById(R.id.button_1);
button.setOnClickListener(new OnClickListener(){
   public void onClick(View v){
      Toast.makeText(FirstActivity.this,"you clicked
       button 1",Toast.LENGTH_SHORT).show();
   }
});

public class MainActivity extends Activity 
implements OnClickListener{
button.setOnClickListener(this);

public void onClick(View v){
   switch(v.getId()){
      case R.id.button:
         imageView.setImageResource(R.drawable.jelly_bean);
         break;
      default:
         break;
   }
}

要約setOnClickListenerパラメータはオブジェクトを受信し、overrideのonClick(View v)メソッドが必要です.