Android Buton usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.