Button之常用事件,button事件
Button之常用事件
一、簡介
1、button介紹
本文介紹了Buttonn的點擊事件,觸摸事件,獲得焦時間點事件
介面分別為:OnClickListener,OnTouchListener,OnFocusChangeListener
這些事件並不是button才會有,別的控制項也會有
2、button類結構
button繼承的TextView,用法也幾乎一樣
二、方法
這個沒啥說的,直接看執行個體吧
三、代碼執行個體
:
前
後
代碼:
fry.Activity01
1 package fry; 2 3 import com.example.buttonDemo1.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.MotionEvent; 8 import android.view.View; 9 import android.view.View.OnClickListener;10 import android.view.View.OnFocusChangeListener;11 import android.view.View.OnTouchListener;12 import android.widget.Button;13 14 public class Activity01 extends Activity implements OnClickListener,OnTouchListener,OnFocusChangeListener {15 private Button btn_one;16 private Button btn_two;17 int value=1;18 19 20 @Override21 protected void onCreate(Bundle savedInstanceState) {22 // TODO Auto-generated method stub23 super.onCreate(savedInstanceState);24 setContentView(R.layout.activity01);25 btn_one = (Button) findViewById(R.id.btn_one);26 btn_two = (Button) findViewById(R.id.btn_two);27 btn_one.setOnClickListener(this);28 btn_two.setOnClickListener(this);//觸摸監聽器29 btn_two.setOnTouchListener(this);30 btn_two.setOnFocusChangeListener(this);31 }32 33 @Override34 public void onClick(View v) {35 // TODO Auto-generated method stub36 Button btn = (Button) v;37 int width = getWindow().getWindowManager().getDefaultDisplay()38 .getWidth();39 /*40 * value等於1表示擴大41 * 等於-1表示縮小42 */43 if (btn.getWidth() < 100&&value==-1) {44 value=1;45 } else if (btn.getWidth() >= width&& value==1) {46 value=-1;47 }48 btn.setWidth(btn.getWidth()+(int)(btn.getWidth()*0.1*value));49 btn.setHeight(btn.getHeight()+(int)(btn.getHeight()*0.1*value));50 51 }52 53 @Override54 public boolean onTouch(View v, MotionEvent event) {55 // TODO Auto-generated method stub56 int action=event.getAction();//事件類型57 if(action==MotionEvent.ACTION_DOWN){//按下58 btn_two.setBackgroundResource(R.drawable.button2);59 }else if(action==MotionEvent.ACTION_UP){//鬆開60 btn_two.setBackgroundResource(R.drawable.button1);61 }62 //如果點擊事件被處理,就傳回true,否則false63 //也就是如果傳true,事件被處理,onClick就不處理這個點擊事件了64 return false;65 }66 67 @Override68 public void onFocusChange(View v, boolean hasFocus) {69 // TODO Auto-generated method stub70 if(hasFocus){//獲得焦點71 btn_two.setBackgroundResource(R.drawable.button2);72 }73 else if(!hasFocus){//失去焦點74 btn_two.setBackgroundResource(R.drawable.button1);75 }76 }77 }
/buttonDemo1/res/layout/activity01.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <Button 8 android:id="@+id/btn_one" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:text="按鈕一"12 ></Button>13 14 <Button 15 android:id="@+id/btn_two"16 android:layout_width="wrap_content"17 android:layout_height="wrap_content"18 android:background="@drawable/button1"19 ></Button>20 21 </LinearLayout>
四、易錯點