android 控件onClick事件的4种实现方式

1、

		TextView tv = (TextView) findViewById(R.id.tv);
		tv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//do something...
			}
		});

2、

		TextView tv = (TextView) findViewById(R.id.tv);
		OnClickListener mOnClickListener = new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//do something...
			}
		};
		tv.setOnClickListener(mOnClickListener);

3、

package com.yqy.yqy_click;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TextView tv = (TextView) findViewById(R.id.tv);
		tv.setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		switch (view.getId()) {
		case R.id.tv:
			//do something...
			break;

		default:
			break;
		}
	}

}

4、


    
	public void onClick(View view){
		//do something...
	}




你可能感兴趣的:(android笔记)