Android編程動態(tài)按鈕實現(xiàn)方法
本文實例講述了Android編程動態(tài)按鈕實現(xiàn)方法。分享給大家供大家參考,具體如下:
第一種: 該方法通過onTouch來實現(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){
//更改為按下時的背景圖片
imageBtn .setImageResource(R.drawable.pressed);
}else if(event.getAction() == MotionEvent.ACTION_UP){
//改為抬起時的圖片
imageBtn .setImageResource(R.drawable.released);
}
return false;
}
};
第二種: 通過XML來實現(xiàn)
用XML文件實現(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>
這個文件放在drawable目錄下面。命名為button_add_x.xml
使用的時候:
<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>
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android布局layout技巧總結》、《Android視圖View技巧總結》、《Android開發(fā)入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
- Android自定義View制作動態(tài)炫酷按鈕實例解析
- 安卓(Android)動態(tài)創(chuàng)建多個按鈕并添加監(jiān)聽事件
- Android按鈕單擊事件的四種常用寫法總結
- Android按鈕按下的時候改變顏色實現(xiàn)方法
- Android實現(xiàn)點擊AlertDialog上按鈕時不關閉對話框的方法
- Android中讓按鈕擁有返回鍵功能的方法及重寫返回鍵功能
- 如何在Android中實現(xiàn)漸顯按鈕的左右滑動效果
- android監(jiān)聽返回按鈕事件的方法
- Android使用自定義alertdialog實現(xiàn)確認退出按鈕
- Android編程動態(tài)加載布局實例詳解【附demo源碼】
- Android滑動動態(tài)分頁實現(xiàn)方法
相關文章
Android ListView與getView調用卡頓問題解決辦法
這篇文章主要介紹了Android ListView與getView調用卡頓問題解決辦法的相關資料,這里提供實例及解決辦法幫助大家解決這種問題,需要的朋友可以參考下2017-08-08
Android實現(xiàn)志愿者系統(tǒng)詳細步驟與代碼
這篇文章主要介紹了Android實現(xiàn)志愿者系統(tǒng),本系統(tǒng)采用MVC架構設計,SQLite數(shù)據表有用戶表、成員表和活動表,有十多個Activity頁面。打開應用,進入歡迎界面,3s后跳轉登錄界面,用戶先注冊賬號,登錄成功后進入主界面2023-02-02
Android中ViewPager實現(xiàn)滑動指示條及與Fragment的配合
這篇文章主要介紹了Android中ViewPager實現(xiàn)滑動指示條及與Fragment的配合,使用Fragment實現(xiàn)ViewPager的滑動是一種比較推薦的做法,需要的朋友可以參考下2016-03-03
Android客制化adb shell進去后顯示shell@xxx的標識
今天小編就為大家分享一篇關于Android客制化adb shell進去后顯示shell@xxx的標識,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android仿Flipboard動畫效果的實現(xiàn)代碼
這篇文章主要介紹了Android仿Flipboard動畫效果的實現(xiàn)代碼,本文圖文并茂給大家介紹的非常詳細,需要的朋友可以參考下2018-01-01

