Android中button的onClick事件几种方法

  package com.example.androidtest;

  import android.os.Bundle;

  import android.app.Activity;

  import android.content.Intent;

  import android.view.Menu;

  import android.widget.Button;

  import android.widget.EditText;

  import android.widget.Toast;

  import android.view.View;

  import android.widget.TextView;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  //import android.text.Editable;

  //import android.text.TextWatcher;

  import android.widget.EditText;

  public class MainActivity extends Activity

  {

  @Override

  protected void onCreate(Bundle savedInstanceState)

  {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  Button button1 = (Button)findViewById(R.id.button1);

  Button button2 = (Button)findViewById(R.id.button2);

  //方法一:

  //增加事件响应

  button1.setOnClickListener(new Button.OnClickListener()

  {

  public void onClick(View v)

  {

  Intent intent =new Intent();

  intent.setClass(MainActivity.this, TestActivity.class);

  MainActivity.this.startActivity(intent);

  }

  });

  button2.setOnClickListener(new Button.OnClickListener()

  {

  public void onClick(View v)

  {

  Intent intent =new Intent();

  intent.setClass(MainActivity.this, TActivity.class);

  MainActivity.this.startActivity(intent);

  }

  });

  }