Android開發(fā)-之監(jiān)聽button點(diǎn)擊事件的多種方法
在Android下,事件的發(fā)生是在監(jiān)聽器下進(jìn)行,android系統(tǒng)可以響應(yīng)按鍵事件和觸摸屏事件,本文主要介紹了button點(diǎn)擊事件的方法
一、實(shí)現(xiàn)button點(diǎn)擊事件的方法
實(shí)現(xiàn)button點(diǎn)擊事件的監(jiān)聽方法有很多種,這里總結(jié)了常用的四種方法:
1、匿名內(nèi)部類
2、外部類(獨(dú)立類)
3、實(shí)現(xiàn)OnClickListener接口
4、添加XML屬性
每一種方法都有它的優(yōu)點(diǎn)也有它的不足,那么接下來就來詳細(xì)的講解這四個實(shí)現(xiàn)方法
二、具體實(shí)現(xiàn)
1、匿名內(nèi)部類:
在Android開發(fā)中我們會經(jīng)??吹礁鞣N匿名內(nèi)部類的使用,那么在實(shí)現(xiàn)button點(diǎn)擊事件的時候也可以用匿名內(nèi)部類。
這樣使用的好處是:
1)不需要重新寫一個類,直接在new的時候去實(shí)現(xiàn)想實(shí)現(xiàn)的方法,很方便。
2)當(dāng)別的地方都用不到這個方法的時候使用匿名內(nèi)部類
3)高內(nèi)聚,高內(nèi)聚是設(shè)計原則之一,匿名內(nèi)部類的特性之一就是擁有高內(nèi)聚。
但是也有不足的地方:
1)當(dāng)別的地方也需要同樣的一個方法時還要重新再在那個地方寫一次匿名內(nèi)部類,這樣使得代碼的冗余性很高。
2)不方便后期的維護(hù)
a、添加一個按鈕
<Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="20dp" android:text="方法一:匿名內(nèi)部類" />
b、回到MainActivity中實(shí)現(xiàn)
public class MainActivity extends Activity{ private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * 方法一:使用匿名內(nèi)部類實(shí)現(xiàn)button按鈕的 */ //綁定button按鈕 btn1 = (Button) findViewById(R.id.button1); //監(jiān)聽button事件 btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast tot = Toast.makeText( MainActivity.this, "匿名內(nèi)部類實(shí)現(xiàn)button點(diǎn)擊事件", Toast.LENGTH_LONG); tot.show(); } }); } }
c、運(yùn)行結(jié)果
2、獨(dú)立類(外部類):
重新寫一個獨(dú)立的類來實(shí)現(xiàn)業(yè)務(wù)邏輯或是想要的效果
這樣寫的好處是:
1)一定情況下可以方便維護(hù)
2)可以降低代碼的冗余性,可以同時使用到多個地方
不足的地方是:
1)當(dāng)只使用一次時浪費(fèi)資源,程序的性能不高
2)當(dāng)有很多個方法時代碼的可讀性不高,此時不方便維護(hù)
a、添加一個button按鈕
<Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button1" android:layout_below="@+id/button1" android:layout_marginTop="20dp" android:text="方法二:獨(dú)立類" />
b、回到MainActivity中實(shí)現(xiàn),外部類中需要實(shí)現(xiàn)OnClickListener接口,并重寫其中的方法
public class MainActivity extends Activity { private Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * 方法二:獨(dú)立類實(shí)現(xiàn)button實(shí)現(xiàn) */ btn2 = (Button) findViewById(R.id.button2); btn2.setOnClickListener(new btn2Click(this)); } }
public class btn2Click implements OnClickListener { private Context context; //重載btn2Click方法 public btn2Click(Context ct){ this.context=ct; } @Override public void onClick(View v) { Toast tot = Toast.makeText( context, "獨(dú)立類實(shí)現(xiàn)button點(diǎn)擊事件", Toast.LENGTH_LONG); tot.show(); } }
c、運(yùn)行效果
3、實(shí)現(xiàn)OnClickListener接口:
與獨(dú)立類實(shí)現(xiàn)的原理是一樣的,優(yōu)點(diǎn)和缺陷也是大徑相同的,實(shí)現(xiàn)OnClickListener接口的時候?qū)崿F(xiàn)它其中的onClick方法
a、添加button按鈕
<Button android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignRight="@+id/button2" android:layout_below="@+id/button2" android:layout_marginTop="20dp" android:text="方法三:實(shí)現(xiàn)接口" />
b、回到MainActivity中實(shí)現(xiàn)
public class MainActivity extends Activity implements OnClickListener { private Button btn3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* * 方法三:實(shí)現(xiàn)OnClickListener接口 */ btn3 = (Button) findViewById(R.id.button3); btn3.setOnClickListener(this); } //實(shí)現(xiàn)OnClickListener接口中的方法 @Override public void onClick(View v) { Toast tot = Toast.makeText( MainActivity.this, "接口OnClickListener實(shí)現(xiàn)button點(diǎn)擊事件", Toast.LENGTH_LONG); tot.show(); } }
c、運(yùn)行效果
4、添加XML屬性:
我們可以給XML添加一個onClick屬性來實(shí)現(xiàn)點(diǎn)擊事件的監(jiān)控
這樣的好處是:更加便捷,代碼量能夠減少
但是不足的地方是:每一次維護(hù)的時候都要去XML里面改源碼,是不是很不好維護(hù)很麻煩呢?
a、添加一個button按鈕,并添加onClick按鈕
<Button android:id="@+id/button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button3" android:layout_below="@+id/button3" android:layout_marginTop="20dp" android:onClick="btn4Click" android:text="方法四:添加xml屬性" />
b、回到MainActivity中實(shí)現(xiàn)onClick按鈕
/* * 方法四:添加XML屬性 */ public void btn4Click(View v){ Toast tot = Toast.makeText( MainActivity.this, "添加XML標(biāo)簽實(shí)現(xiàn)button點(diǎn)擊事件", Toast.LENGTH_LONG); tot.show(); }
c、運(yùn)行結(jié)果:
三、總結(jié):
1、在實(shí)現(xiàn)監(jiān)聽的時候都是需要兩步走:
1)綁定button按鈕
2)監(jiān)聽button事件
1、具體使用什么方法去實(shí)現(xiàn)button按鈕點(diǎn)擊事件的監(jiān)聽要看具體的需求,都各有各的好處和不足。如果只使用一次則推薦使用內(nèi)部類的方法;如果多次使用則使用外部類的方法;實(shí)現(xiàn)接口的方法可以在原本的類中實(shí)現(xiàn);但是在XML里面添加屬性的方法時不推薦的,畢竟很不好去維護(hù)。
2、內(nèi)部類的使用在Android開發(fā)中是經(jīng)常用到的,所以非常的重要。java中關(guān)于內(nèi)部類的使用詳解:http://www.dbjr.com.cn/article/36125.htm
3、Android開發(fā)中有很多按鈕,但是監(jiān)聽的方法常用的都是這幾種,所以能夠舉一反三,這也是為什么寫這個的原因
4、Toast是Android中一個實(shí)現(xiàn)的效果,是不是經(jīng)??吹竭@個效果呢?在寫監(jiān)聽的時候順便也把Toast一起學(xué)了,一舉兩得哈哈哈??!
PS:看似零零散散的知識點(diǎn),但所有的知識點(diǎn)都是一條連線的,任何行業(yè)的知識點(diǎn)都是如此,就好比先有了數(shù)字才出現(xiàn)算數(shù),先出現(xiàn)了英文字母才有了單詞一樣。。。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android開發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android 點(diǎn)擊ImageButton時有“按下”的效果的實(shí)現(xiàn)
- Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
- Android 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 CardView詳解及使用方法和實(shí)例
這篇文章主要介紹了Android CardView詳解及使用方法和實(shí)例的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12Android?Studio實(shí)現(xiàn)彈窗設(shè)置
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)彈窗設(shè)置,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Android開發(fā)一行代碼解決安卓重復(fù)點(diǎn)擊
這篇文章主要為大家介紹了Android開發(fā)一行代碼解決安卓重復(fù)點(diǎn)擊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Android編程監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)改變的方法
這篇文章主要介紹了Android編程監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)改變的方法,基于BroadcastReceiver實(shí)現(xiàn)針對網(wǎng)絡(luò)連接狀態(tài)的監(jiān)聽功能,需要的朋友可以參考下2017-06-06Android 獲取正在運(yùn)行的任務(wù)和服務(wù)的小例子
Android 獲取正在運(yùn)行的任務(wù)和服務(wù)的小例子,需要的朋友可以參考一下2013-05-05詳解OpenGL Shader彩虹條紋效果的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何通過OpenGL Shader實(shí)現(xiàn)彩虹條紋效果,最后的效果和圖片處理軟件colorow中的彩虹效果濾鏡相似,需要的可以參考一下2022-02-02