Android編程動(dòng)態(tài)按鈕實(shí)現(xiàn)方法
本文實(shí)例講述了Android編程動(dòng)態(tài)按鈕實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
第一種: 該方法通過onTouch來實(shí)現(xiàn),
btn3 = (ImageButton) findViewById(R.id.ImageButton03); btn3.setOnTouchListener(touchListener3); View.OnTouchListener touchListener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ImageButton imageBtn = (ImageButton) v; if(event.getAction() == MotionEvent.ACTION_DOWN){ //更改為按下時(shí)的背景圖片 imageBtn .setImageResource(R.drawable.pressed); }else if(event.getAction() == MotionEvent.ACTION_UP){ //改為抬起時(shí)的圖片 imageBtn .setImageResource(R.drawable.released); } return false; } };
第二種: 通過XML來實(shí)現(xiàn)
用XML文件實(shí)現(xiàn):
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false" android:drawable="@drawable/button_add" /> <item android:state_pressed="true" android:drawable="@drawable/button_add_pressed" /> <item android:state_focused="true" android:drawable="@drawable/button_add_pressed" /> <item android:drawable="@drawable/button_add" /> </selector>
這個(gè)文件放在drawable目錄下面。命名為button_add_x.xml
使用的時(shí)候:
<ImageButton android:id="@+id/ImageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:src="@drawable/button_add_x" > </ImageButton>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android自定義View制作動(dòng)態(tài)炫酷按鈕實(shí)例解析
- 安卓(Android)動(dòng)態(tài)創(chuàng)建多個(gè)按鈕并添加監(jiān)聽事件
- Android按鈕單擊事件的四種常用寫法總結(jié)
- Android按鈕按下的時(shí)候改變顏色實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android中讓按鈕擁有返回鍵功能的方法及重寫返回鍵功能
- 如何在Android中實(shí)現(xiàn)漸顯按鈕的左右滑動(dòng)效果
- android監(jiān)聽返回按鈕事件的方法
- Android使用自定義alertdialog實(shí)現(xiàn)確認(rèn)退出按鈕
- Android編程動(dòng)態(tài)加載布局實(shí)例詳解【附demo源碼】
- Android滑動(dòng)動(dòng)態(tài)分頁實(shí)現(xiàn)方法
相關(guān)文章
SQLiteStudio優(yōu)雅調(diào)試Android手機(jī)數(shù)據(jù)庫Sqlite(推薦)
這篇文章主要介紹了SQLiteStudio優(yōu)雅調(diào)試Android手機(jī)數(shù)據(jù)庫Sqlite的相關(guān)資料,需要的朋友可以參考下2017-11-11Android應(yīng)用啟動(dòng)速度優(yōu)化
這篇文章主要介紹了Android應(yīng)用啟動(dòng)速度優(yōu)化的相關(guān)資料,需要的朋友可以參考下2016-01-01Android中TextView顯示插入的圖片實(shí)現(xiàn)方法
這篇文章主要介紹了Android中TextView顯示插入的圖片實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了TextView三種顯示插入圖片的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08android異步加載圖片并緩存到本地實(shí)現(xiàn)方法
圖片過多造成內(nèi)存溢出,這個(gè)是最不容易解決的,要想一些好的緩存策略,比如大圖片使用LRU緩存策略或懶加載緩存策略。今天首先介紹一下本地緩存圖片2013-01-01TextView長(zhǎng)按復(fù)制的實(shí)現(xiàn)方法(總結(jié))
下面小編就為大家?guī)硪黄猅extView長(zhǎng)按復(fù)制的實(shí)現(xiàn)方法(總結(jié))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄
這篇文章主要介紹了Android App仿QQ制作Material Design風(fēng)格沉浸式狀態(tài)欄的實(shí)例,同時(shí)也給出了4.4版本下實(shí)現(xiàn)效果與5.0的對(duì)比,需要的朋友可以參考下2016-04-04Android編程中TextView寬度過大導(dǎo)致Drawable無法居中問題解決方法
這篇文章主要介紹了Android編程中TextView寬度過大導(dǎo)致Drawable無法居中問題解決方法,以實(shí)例形式較為詳細(xì)的分析了TextView設(shè)置及xml布局與調(diào)用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10