Android自定義Button并設(shè)置不同背景圖片的方法
本文實(shí)例講述了Android自定義Button并設(shè)置不同背景圖片的方法。分享給大家供大家參考,具體如下:
1、自定義MyButton類
public class MyButton extends Button { //This constructormust be public MyButton(Context context, AttributeSet attrs) { super(context, attrs); } public MyButton(Context context) { super(context); } private Paint mPaint = null; private String mText; private int mX, mY; public void onSetText(String text, int nLeft, int nBottom, int nTextSize, int nTextColor) { mPaint = new Paint(); mPaint.setTextSize(nTextSize); mPaint.setColor(nTextColor); this.mText = text; this.mX = nLeft; this.mY = nBottom; } private int mDownBmpId, mUpBmpId; public void onSetBmp(int nDownID, int nUpID) { this.mDownBmpId = nDownID; this.mUpBmpId = nUpID; } @Override public void onDraw(Canvas canvas) { if (mPaint != null) canvas.drawText(mText, mX, mY, mPaint); super.onDraw(canvas); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { super.setBackgroundResource(mDownBmpId); } else if (event.getAction() == MotionEvent.ACTION_UP) { super.setBackgroundResource(mUpBmpId); } return super.onTouchEvent(event); } }
2、 在xml布局文件中添加MyButton控件,像應(yīng)用普通的Button控件一樣。
<com.MyButton android:id="@+id/test_btn" android:layout_width="120px" android:layout_height="fill_parent" android:text="Test" android:background="@drawable/btn_u" />
其中com.MyButton是你定義的MyButton類所在的包名
3、在onCreate()中加載MyButton控件。
MyButton btn = (MyButton)findViewById(R.id.test_btn); btn.onSetBmp(R.drawable.btn_d, R.drawable.btn_u);
其中btn_d表示為按下btn時(shí)背景圖片,btn_u為默認(rèn)狀態(tài)下btn背景圖片
更多關(guān)于Android控件相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)手機(jī)壁紙改變的方法
- Android編程之動(dòng)態(tài)壁紙實(shí)例分析
- android動(dòng)態(tài)壁紙調(diào)用的簡單實(shí)例
- android 自定義ScrollView實(shí)現(xiàn)背景圖片伸縮的實(shí)現(xiàn)代碼及思路
- android中實(shí)現(xiàn)背景圖片顏色漸變方法
- Android設(shè)置桌面背景圖片的實(shí)現(xiàn)方法
- Android 背景圖片的縮放實(shí)現(xiàn)
- Android編程之書架效果背景圖處理方法
- Android編程之手機(jī)壁紙WallPaper設(shè)置方法示例
相關(guān)文章
Android實(shí)現(xiàn)文字翻轉(zhuǎn)動(dòng)畫的效果
本文實(shí)現(xiàn)了Android程序文字翻轉(zhuǎn)動(dòng)畫的實(shí)現(xiàn),具有一定的參考價(jià)值,有需要的朋友可以了解一下。2016-10-10Android使用post方式上傳圖片到服務(wù)器的方法
這篇文章主要介紹了Android使用post方式上傳圖片到服務(wù)器的方法,結(jié)合實(shí)例形式分析了Android文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2016-03-03Android Studio 4.1沒有GsonFormat插件的解決
這篇文章主要介紹了Android Studio 4.1沒有GsonFormat插件的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Android編程實(shí)現(xiàn)自定義ImageView圓圖功能的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)自定義ImageView圓圖功能的方法,結(jié)合實(shí)例形式分析了Android自定義ImageView及實(shí)現(xiàn)圓圖效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android自定義View繪圖實(shí)現(xiàn)拖影動(dòng)畫
這篇文章主要介紹了Android自定義View繪圖實(shí)現(xiàn)拖影動(dòng)畫,,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android編程實(shí)現(xiàn)輸入框動(dòng)態(tài)自動(dòng)提示功能
這篇文章主要介紹了Android編程實(shí)現(xiàn)輸入框動(dòng)態(tài)自動(dòng)提示功能,結(jié)合實(shí)例形式分析了AutoCompleteTextView相關(guān)使用技巧,需要的朋友可以參考下2017-03-03Android中ListView的幾種常見的優(yōu)化方法總結(jié)
Android中的ListView應(yīng)該算是布局中幾種最常用的組件之一,本篇文章主要做了三種優(yōu)化總結(jié),有興趣的可以了解一下。2017-02-02