This topic describes the OnClickListener and OnTouchListener events of a button in android.
Running result:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + MS4gYWN0aXZpdHlfbWFpbi54bWw8L3A + CjxwcmUgY2xhc3M9 "brush: java;">
2. MainActvity. java
Package com. android. button1; import android. OS. bundle; import android. annotation. suppressLint; import android. app. activity; import android. view. gravity; import android. view. keyEvent; import android. view. menu; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onFocusChangeListener; import android. view. view. onKeyListener; import android. view. view. onTouchListener; import android. widget. button; import android. widget. toast; @ SuppressLint ("ShowToast") public class MainActivity extends Activity implements OnClickListener, OnTouchListener {private int value = 1; // used to change the size of the private Button commonButton; private Button imageButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); commonButton = (Button) this. findViewById (R. id. commonbutton); imageButton = (Button) this. findViewById (R. id. imagebutton); commonButton. setOnClickListener (this); imageButton. setOnClickListener (this); imageButton. setOnTouchListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubButton button = (Button) v; if (value = 1 & button. getWidth () = getWindowManager (). getdefadisplay display (). getWidth () {value =-1;} else if (value =-1 & button. getWidth () <100) {value = 1;} button. setWidth (button. getWidth () + (int) (button. getWidth () * 0.1) * value); button. setHeight (button. getHeight () + (int) (button. getWidth () * 0.1) * value);} @ Overridepublic boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stubif (event. getAction () = MotionEvent. ACTION_UP) {imageButton. setBackgroundResource (R. drawable. button2);} else if (event. getAction () = MotionEvent. ACTION_DOWN) {imageButton. setBackgroundResource (R. drawable. button3) ;}return false ;}}
Project directory:
Zookeeper