Button之常用事件,button事件

來源:互聯網
上載者:User

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>

 

 

 

四、易錯點

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.