Android自定義button點擊效果的兩種方式
我們在界面上經(jīng)常會用到button按鈕,但通常button點擊后看不到點擊的效果,如果用戶連續(xù)點擊了兩次,就會報NAR錯誤,這樣交互性就比較差了。如果我們自定義了button點擊效果,比如我們點擊了button能讓我們看到我們確實點擊了button按鈕,這樣就會有效的避免重復點擊了。
自定義點擊效果有兩種方式,一種是在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):一種是按下 一種是獲得焦點。
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(), "你點擊了button按鈕", Toast.LENGTH_SHORT).show();
}
});
下面看下點擊效果:
點擊button前:

當按下button按鈕時:
接下來 看下第二種實現(xiàn)方式,在代碼中實現(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中實現(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)聽,因為OnClickListener繼承了OnTouchListener。運行效果和上面一樣,這里不做過多解釋。
以上就是Android自定義button點擊效果實現(xiàn)方式的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Android開發(fā)設(shè)置RadioButton點擊效果的方法
- Android 點擊ImageButton時有“按下”的效果的實現(xiàn)
- Android懸浮按鈕點擊返回頂部FloatingActionButton
- Android Button按鈕的四種點擊事件
- Android開發(fā)-之監(jiān)聽button點擊事件的多種方法
- Android 自定義Button控件實現(xiàn)按鈕點擊變色
- Android中button點擊后字體的變色效果
- Android開發(fā)之創(chuàng)建可點擊的Button實現(xiàn)方法
- Android實現(xiàn)點擊Button產(chǎn)生水波紋效果
- Android Button點擊事件的四種實現(xiàn)方法
相關(guān)文章
Android實現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法詳解
這篇文章主要介紹了Android實現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法,結(jié)合實例形式詳細分析了Activity、Service與Broadcaster三大組件相互調(diào)用的原理,實現(xiàn)方法與相關(guān)注意事項,需要的朋友可以參考下2016-03-03
Android中Listview點擊item不變顏色及設(shè)置listselector 無效的解決方案
這篇文章主要介紹了Android中Listview點擊item不變顏色及設(shè)置listselector 無效的原因及解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
Android開發(fā)實現(xiàn)按鈕點擊切換背景并修改文字顏色的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)按鈕點擊切換背景并修改文字顏色的方法,涉及Android界面布局與相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2018-01-01
Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評分示例
本篇文章主要介紹了Android 應(yīng)用中跳轉(zhuǎn)到應(yīng)用市場評分示例,非常具有實用價值,需要的朋友可以參考下。2017-02-02
Android 中TextView中跑馬燈效果的實現(xiàn)方法
這篇文章主要介紹了Android 中TextView中跑馬燈效果的實現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02

