Android Button按鈕的四種點(diǎn)擊事件
本文實(shí)例為大家分享了安卓Button按鈕的四種點(diǎn)擊事件,供大家參考,具體內(nèi)容如下
第一種:內(nèi)部類實(shí)現(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è)置一個(gè)點(diǎn)擊事件
btn.setOnClickListener(new MyClickListener()) //傳入的是ClickListener參數(shù)所以我們必須去定義一個(gè)參數(shù)接口
4.定義一個(gè)類去實(shí)現(xiàn) 按鈕需要的接口類型
public MianActivity extend Activity(){ ... ... private class MyClickListener()implent OnclickListener{ //當(dāng)按鈕被點(diǎn)擊的時(shí)候調(diào)用 public void Onclick (View v){ //這里寫點(diǎn)擊事件方法 System.out.printLn("被點(diǎn)擊了") } } }
第二種:利用匿名內(nèi)部類來實(shí)現(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è)置一個(gè)點(diǎn)擊事件
//匿名內(nèi)部類 public MianActivity extend Activity(){ ... ... btn.setOnClickListener(new OnClickListener(){ public void Onclick (View v){ //這里寫點(diǎn)擊事件方法 System.out.printLn("被點(diǎn)擊了") } } ) };
第三種:Activity實(shí)現(xiàn)OnclickListener接口適用于多個(gè)按鈕情況
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è)置一個(gè)點(diǎn)擊事件
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){ //具體判斷點(diǎn)擊的是哪個(gè)按鈕 switch(v.getId()){ case.R.id.button1://代表點(diǎn)擊第一個(gè)按鈕 TODO();//實(shí)現(xiàn)具體方法 break; case.R.id.button2: TODO();//實(shí)現(xiàn)具體方法 break; case.R.id.button3: TODO();//實(shí)現(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.聲明一個(gè)方法,方法名和你要點(diǎn)擊的這個(gè)按鈕在xml布局中聲明的Onclick屬性一樣
public void **click**(View v){ TODO();//實(shí)現(xiàn)具體方法 }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)
- Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
- Android開發(fā)-之監(jiān)聽button點(diǎn)擊事件的多種方法
- Android 自定義Button控件實(shí)現(xiàn)按鈕點(diǎn)擊變色
- Android中button點(diǎn)擊后字體的變色效果
- Android自定義button點(diǎn)擊效果的兩種方式
- Android開發(fā)之創(chuàng)建可點(diǎn)擊的Button實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)點(diǎn)擊Button產(chǎn)生水波紋效果
- Android Button點(diǎn)擊事件的四種實(shí)現(xiàn)方法
相關(guān)文章
android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件
本篇文章主要介紹了android為L(zhǎng)istView每個(gè)Item上面的按鈕添加事件,有興趣的同學(xué)可以了解一下。2016-11-11Android開發(fā)中使用外部應(yīng)用獲取SD卡狀態(tài)的方法
這篇文章主要介紹了Android開發(fā)中使用外部應(yīng)用獲取SD卡狀態(tài)的方法,簡(jiǎn)單分析了Android監(jiān)聽SD卡狀態(tài)的方法,并結(jié)合實(shí)例形式分析了Android外部應(yīng)用獲取SD卡狀態(tài)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Android登陸界面實(shí)現(xiàn)清除輸入框內(nèi)容和震動(dòng)效果
這篇文章主要介紹了Android登陸界面實(shí)現(xiàn)清除輸入框內(nèi)容和震動(dòng)效果,感興趣的小伙伴們可以參考一下2015-12-12Android Socket服務(wù)端與客戶端用字符串的方式互相傳遞圖片的方法
這篇文章主要介紹了Android Socket服務(wù)端與客戶端用字符串的方式互相傳遞圖片的方法的相關(guān)資料,需要的朋友可以參考下2016-05-05Android下Activity間通信序列化過程中的深淺拷貝淺析
這篇文章主要給大家介紹了關(guān)于Android下Activity間通信序列化過程中深淺拷貝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10android橫豎屏切換時(shí)候Activity的生命周期
曾經(jīng)遇到過一個(gè)面試題,讓你寫出橫屏切換豎屏Activity的生命周期?,F(xiàn)在給大家分析一下他切換時(shí)具體的生命周期是怎么樣的2013-01-01Android實(shí)現(xiàn)類似于PC中的右鍵彈出菜單效果
這篇文章主要介紹了Android實(shí)現(xiàn)類似于PC中的右鍵彈出菜單效果,需要的朋友可以參考下2015-12-12