Android的四种点击事件

第一种;匿名内部类


	
package com.example.dianjishijian;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
	private Button btn1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1=(Button)findViewById(R.id.btn1);
                //匿名内部类
		btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, "按钮被点击", 0).show();
				
			}
		});
	}
}

第二种:通过Activity实现OnClickListener接口


	


package com.example.dianjishijian;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener{
	private Button btn1;
	private Button btn2;
	private Button btn3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1=(Button)findViewById(R.id.btn1);
		btn2=(Button)findViewById(R.id.btn2);
		btn3=(Button)findViewById(R.id.btn3);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn1:
			Toast.makeText(MainActivity.this,"btn1被点击", 0).show();
			break;
		case R.id.btn2:
			Toast.makeText(MainActivity.this,"btn2被点击", 0).show();
			break;
		case R.id.btn3:
			Toast.makeText(MainActivity.this,"btn3被点击", 0).show();
			break;
		default:
			break;
		}
		
	}
}

第三种:


	
package com.example.dianjishijian;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity{
	private Button btn1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1=(Button)findViewById(R.id.btn1);
	}	
	
	public void click(View v){
		Toast.makeText(MainActivity.this, "btn1被点击", 0).show();
	}
}

第四种点击事件


	
package com.example.dianjishijian;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity{
	private Button btn1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1=(Button)findViewById(R.id.btn1);
		btn1.setOnClickListener(new MyListener());
	}	
	
	public class MyListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			Toast.makeText(MainActivity.this, "btn1被点击", 0).show();
			
		}
		
	}
}


你可能感兴趣的:(android)