国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Button按鈕的四種點擊事件

2019-12-12 04:01:45
字體:
供稿:網(wǎng)友

本文實例為大家分享了安卓Button按鈕的四種點擊事件,供大家參考,具體內(nèi)容如下

第一種:內(nèi)部類實現(xiàn)

1.xml里面先設(shè)置Button屬性

<Button android:id="+@id/button1"; android:layout_width="wrap_parent"; android:layout_height="wrap_parent" android:text="按鈕"/>

2.找到按鈕

Button btn =(Button)findViewById(R.layout.button1)

3.給Button設(shè)置一個點擊事件

btn.setOnClickListener(new MyClickListener()) //傳入的是ClickListener參數(shù)所以我們必須去定義一個參數(shù)接口

4.定義一個類去實現(xiàn) 按鈕需要的接口類型

public MianActivity extend Activity(){......private class MyClickListener()implent OnclickListener{ //當(dāng)按鈕被點擊的時候調(diào)用 public void Onclick (View v){  //這里寫點擊事件方法  System.out.printLn("被點擊了")  }} }

第二種:利用匿名內(nèi)部類來實現(xiàn)

1.xml里面先設(shè)置Button屬性

<Button android:id="+@id/button1"; android:layout_width="wrap_parent"; android:layout_height="wrap_parent" android:text="按鈕"/>

2.找到按鈕

Button btn =(Button)findViewById(R.layout.button1);

3.給Button設(shè)置一個點擊事件

//匿名內(nèi)部類public MianActivity extend Activity(){......btn.setOnClickListener(new OnClickListener(){ public void Onclick (View v){  //這里寫點擊事件方法  System.out.printLn("被點擊了")  }} )  };

第三種:Activity實現(xiàn)OnclickListener接口適用于多個按鈕情況

1.xml里面先設(shè)置Button屬性

<Button android:id="+@id/button1"; android:layout_width="wrap_parent"; android:layout_height="wrap_parent" android:text="按鈕"/><Button android:id="+@id/button2"; android:layout_width="wrap_parent"; android:layout_height="wrap_parent" android:text="按鈕2"/> <Button android:id="+@id/button1"; android:layout_width="wrap_parent"; android:layout_height="wrap_parent" android:text="按鈕3"/>

2.找到按鈕

Button btn =(Button)findViewById(R.layout.button1)Button btn2 =(Button)findViewById(R.layout.button2)Button btn3 =(Button)findViewById(R.layout.button3)

3.給Button設(shè)置一個點擊事件

public MianActivity extend Activity implement OnClickListener(){  ...  ...  Button btn =(Button)findViewById(this);//this代表MainActivity  Button btn2 =(Button)findViewById(this)  Button btn3 =(Button)findViewById(this)  public void Onclick (View v){  //具體判斷點擊的是哪個按鈕  switch(v.getId()){  case.R.id.button1://代表點擊第一個按鈕   TODO();//實現(xiàn)具體方法   break;  case.R.id.button2:   TODO();//實現(xiàn)具體方法   break;  case.R.id.button3:   TODO();//實現(xiàn)具體方法   break;    default:   break;  }  }  private void TODO(){   //具體方法  }}

第四種:在xml里面聲明onclick

1.xml里面先設(shè)置Button屬性

<Button android:id="+@id/*button1*"; android:layout_width="wrap_parent"; android:layout_height="wrap_parent" android:text="按鈕" android:onClick="click"/>

2.找到按鈕

Button btn =(Button)findViewById(R.layout.button1)

3.聲明一個方法,方法名和你要點擊的這個按鈕在xml布局中聲明的Onclick屬性一樣

public void **click**(View v){ TODO();//實現(xiàn)具體方法}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 长顺县| 丹东市| 咸丰县| 梁河县| 临湘市| 天祝| 北流市| 顺平县| 英超| 乌拉特后旗| 娱乐| 祁东县| 枝江市| 孙吴县| 犍为县| 霍城县| 钦州市| 盱眙县| 文登市| 化隆| 婺源县| 雷山县| 榆中县| 嵊泗县| 南安市| 阳新县| 罗甸县| 泸州市| 九龙县| 奉节县| 远安县| 新平| 晋宁县| 临海市| 利川市| 铜梁县| 萍乡市| 遵义市| 鲁甸县| 潜山县| 扶沟县|