Android自定義button點(diǎn)擊效果的兩種方式
我們?cè)诮缑嫔辖?jīng)常會(huì)用到button按鈕,但通常button點(diǎn)擊后看不到點(diǎn)擊的效果,如果用戶連續(xù)點(diǎn)擊了兩次,就會(huì)報(bào)NAR錯(cuò)誤,這樣交互性就比較差了。如果我們自定義了button點(diǎn)擊效果,比如我們點(diǎn)擊了button能讓我們看到我們確實(shí)點(diǎn)擊了button按鈕,這樣就會(huì)有效的避免重復(fù)點(diǎn)擊了。
自定義點(diǎn)擊效果有兩種方式,一種是在xml中定義,另一種是在代碼中定義。
首先看一下如何在xml中定義:
在drawable下新建selector.xml文件:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_press" android:state_pressed="true"/> <item android:drawable="@drawable/button_nomal" android:state_focused="false" android:state_pressed="false"/> <item android:drawable="@drawable/button_focus" android:state_focused="true"/> <item android:drawable="@drawable/button_nomal" android:state_focused="false"/> </selector>
定義了兩種狀態(tài):一種是按下 一種是獲得焦點(diǎn)。
drawable分別引用了這三張圖片
然后在main.xml下添加button按鈕
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button效果演示" android:background="@drawable/selector" />
在MainActivtiy中得到button
Button button1=(Button) this.findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "你點(diǎn)擊了button按鈕", Toast.LENGTH_SHORT).show(); } });
下面看下點(diǎn)擊效果:
點(diǎn)擊button前:
當(dāng)按下button按鈕時(shí):
接下來 看下第二種實(shí)現(xiàn)方式,在代碼中實(shí)現(xiàn):
首先在main.xml中添加:
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button效果演示" android:background="@drawable/button_nomal"/>
接下面在MainActivity中實(shí)現(xiàn):
Button button2=(Button) this.findViewById(R.id.button2); button2.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if(event.getAction()==MotionEvent.ACTION_DOWN){ v.setBackgroundResource(R.drawable.button_press); }else if(event.getAction()==MotionEvent.ACTION_UP){ v.setBackgroundResource(R.drawable.button_nomal); } return false; } });
在這類綁定了button的OnTouchListener監(jiān)聽,因?yàn)镺nClickListener繼承了OnTouchListener。運(yùn)行效果和上面一樣,這里不做過多解釋。
以上就是Android自定義button點(diǎn)擊效果實(shí)現(xiàn)方式的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android開發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android 點(diǎn)擊ImageButton時(shí)有“按下”的效果的實(shí)現(xiàn)
- Android懸浮按鈕點(diǎn)擊返回頂部FloatingActionButton
- Android Button按鈕的四種點(diǎn)擊事件
- Android開發(fā)-之監(jiān)聽button點(diǎn)擊事件的多種方法
- Android 自定義Button控件實(shí)現(xiàn)按鈕點(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中將一個(gè)圖片切割成多個(gè)圖片的實(shí)現(xiàn)方法
有種場(chǎng)景,我們想將一個(gè)圖片切割成多個(gè)圖片。比如我們?cè)陂_發(fā)一個(gè)拼圖的游戲,就首先要對(duì)圖片進(jìn)行切割2013-05-05Android Flutter圖片處理之高斯模糊的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何利用Android Flutter實(shí)現(xiàn)高斯模糊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08Android AsyncTask 后監(jiān)聽異步加載完畢的動(dòng)作詳解
這篇文章主要介紹了Android 使用AsyncTask 后監(jiān)聽異步加載完畢的動(dòng)作的相關(guān)資料,需要的朋友可以參考下2016-11-11Android幀式布局實(shí)現(xiàn)自動(dòng)切換顏色
這篇文章主要為大家詳細(xì)介紹了Android幀式布局實(shí)現(xiàn)自動(dòng)切換顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04android中實(shí)現(xiàn)editext搜索完成自動(dòng)關(guān)閉軟鍵盤
在Android應(yīng)用開發(fā)中,經(jīng)常會(huì)遇到需要在EditText中輸入內(nèi)容,并通過搜索按鈕進(jìn)行搜索的場(chǎng)景,通常情況下,當(dāng)用戶點(diǎn)擊搜索按鈕后,我們希望關(guān)閉軟鍵盤以提供更好的用戶體驗(yàn),本文將介紹如何在Android中實(shí)現(xiàn)EditText搜索完成后自動(dòng)關(guān)閉軟鍵盤的功能2023-10-10Android自定義View實(shí)現(xiàn)仿駕考寶典顯示分?jǐn)?shù)效果(收藏)
本文通過自定義view和屬性動(dòng)畫結(jié)合在一起實(shí)現(xiàn)實(shí)現(xiàn)仿駕考寶典顯示分?jǐn)?shù)效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-03-03Android側(cè)滑菜單控件DrawerLayout使用詳解
這篇文章主要為大家詳細(xì)介紹了Android側(cè)滑菜單控件DrawerLayout的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12深入解析Android系統(tǒng)中應(yīng)用程序前后臺(tái)切換的實(shí)現(xiàn)要點(diǎn)
這篇文章主要介紹了Android系統(tǒng)中應(yīng)用程序前后臺(tái)切換的實(shí)現(xiàn)要點(diǎn),除了切換操作的效果之外還重點(diǎn)講解了判斷程序運(yùn)行于前臺(tái)還是后臺(tái)的方法,需要的朋友可以參考下2016-04-04