Android Button监听的一种常用方法
package zhou.hong.game;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button1,button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new buttonOnClickListener());
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(new buttonOnClickListener());
}
class buttonOnClickListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent1);
break;
case R.id.button2:
Intent intent2 = new Intent();
intent2.setClass(MainActivity.this, AnotherActivity.class);
startActivity(intent2);
break;
default:
break;
}
}
}
}