代码实现的功能是使得一个按钮在按下的实现显示光色图安,抬起的时候的显示黄色图案。
然后图案自己设置形状。当然还可以缺一角什么的。
为Button绑定 OnTouchListener 监听器。
public class AppMain extends Activity{ private Button mButton; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ButtonListener b = new ButtonListener(); mButton = (Button)findViewById(R.id.button1); mButton.setOnClickListener(b); mButton.setOnTouchListener(b); mButton.setBackgroundResource(R.drawable.green); } class ButtonListener implements OnClickListener, OnTouchListener{ public void onClick(View v) { if(v.getId() == R.id.button1){ Log.d("test", "cansal button ---> click"); } } public boolean onTouch(View v, MotionEvent event) { if(v.getId() == R.id.button1){ if(event.getAction() == MotionEvent.ACTION_UP){ Log.d("test", "cansal button ---> cancel"); mButton.setBackgroundResource(R.drawable.green); } if(event.getAction() == MotionEvent.ACTION_DOWN){ Log.d("test", "cansal button ---> down"); mButton.setBackgroundResource(R.drawable.yellow); } } return false; } } }