1. 程式人生 > >Butterknife @OnClick繫結多控制元件id

Butterknife @OnClick繫結多控制元件id

package com.example.hh;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.BindColor;
import butterknife.BindDimen;
import butterknife.BindDrawable;
import butterknife.BindString;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends Activity {

	@Bind(R.id.tv_text1)
	TextView tv_text1;

	@Bind(R.id.tv_text2)
	TextView tv_text2;

	@Bind(R.id.tv_text3)
	TextView tv_text3;

	@Bind(R.id.tv_text4)
	TextView tv_text4;

	

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ButterKnife.bind(MainActivity.this);
		tv_text1.setText("1231");
		tv_text2.setText("1024");
		tv_text3.setText("5454");
		tv_text4.setText("7865");


	}

	// @OnClick(R.id.tv_text1)
	// public void sayHi(TextView button) {
	// button.setText("你按到我了啦!");
	// }
	//
	// @SuppressLint("NewApi")
	// @OnClick(R.id.tv_text2)
	// public void sayHi2(TextView button) {
	// button.setBackground(graphic);
	// }
	//
	// @OnClick(R.id.tv_text3)
	// public void sayHi3(TextView button) {
	// button.setTextColor(red);
	// }
	//
	// @OnClick(R.id.tv_text4)
	// public void sayHi4(TextView button) {
	// button.setTextSize(spacers);
	// }
	@OnClick({ R.id.tv_text1, R.id.tv_text2, R.id.tv_text3, R.id.tv_text4 })
	public void sayHi(TextView button) {
		switch (button.getId()) {
		case R.id.tv_text1:
			button.setText("你按到我了啦1!");
			Toast.makeText(this, "你按到我了啦1!", Toast.LENGTH_SHORT).show();
			break;
		case R.id.tv_text2:
			button.setText("你按到我了啦2!");
			Toast.makeText(this, "你按到我了啦2!", Toast.LENGTH_SHORT).show();
			break;
		case R.id.tv_text3:
			button.setText("你按到我了啦3!");
			Toast.makeText(this, "你按到我了啦3!", Toast.LENGTH_SHORT).show();
			break;
		case R.id.tv_text4:
			button.setText("你按到我了啦4!");
			Toast.makeText(this, "你按到我了啦4!", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}

}