Android開(kāi)發(fā)之自定義控件用法詳解
本文實(shí)例講述了Android開(kāi)發(fā)之自定義控件用法。分享給大家供大家參考,具體如下:
今天和大家分享下組合控件的使用。很多時(shí)候android自定義控件并不能滿足需求,如何做呢?很多方法,可以自己繪制一個(gè),可以通過(guò)繼承基礎(chǔ)控件來(lái)重寫(xiě)某些環(huán)節(jié),當(dāng)然也可以將控件組合成一個(gè)新控件,這也是最方便的一個(gè)方法。今天就來(lái)介紹下如何使用組合控件,將通過(guò)兩個(gè)實(shí)例來(lái)介紹。
第一個(gè)實(shí)現(xiàn)一個(gè)帶圖片和文字的按鈕,如圖所示:
整個(gè)過(guò)程可以分四步走。第一步,定義一個(gè)layout,實(shí)現(xiàn)按鈕內(nèi)部的布局。代碼如下:
custom_button.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:paddingLeft="10.0dip" android:paddingTop="10.0dip" android:paddingBottom="10.0dip" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_marginLeft="8dip" android:layout_gravity="center_vertical" android:paddingLeft="5.0dip" android:paddingTop="10.0dip" android:paddingBottom="10.0dip" android:paddingRight="10.0dip" android:textSize="18.0sp" /> </LinearLayout>
這個(gè)xml實(shí)現(xiàn)一個(gè)左圖右字的布局,接下來(lái)寫(xiě)一個(gè)類(lèi)繼承LinearLayout,導(dǎo)入剛剛的布局,并且設(shè)置需要的方法,從而使的能在代碼中控制這個(gè)自定義控件內(nèi)容的顯示。代碼如下:
CustomButton.java
package com.szy.customview; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class CustomButton extends LinearLayout { private ImageView iv; private TextView tv; public CustomButton(Context context) { this(context, null); } public CustomButton(Context context, AttributeSet attrs) { super(context, attrs); // 導(dǎo)入布局 LayoutInflater.from(context).inflate(R.layout.custom_button, this, true); iv = (ImageView) findViewById(R.id.iv); tv = (TextView) findViewById(R.id.tv); } /** * 設(shè)置圖片資源 */ public void setImageResource(int resId) { iv.setImageResource(resId); } /** * 設(shè)置顯示的文字 */ public void setTextViewText(String text) { tv.setText(text); } }
第三步,在需要使用這個(gè)自定義控件的layout中加入這控件,只需要在xml中加入即可。方法如下:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.szy.customview.CustomButton android:id="@+id/bt_confirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_bg" /> <com.szy.customview.CustomButton android:id="@+id/bt_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_bg" /> </LinearLayout>
注意的是,控件標(biāo)簽使用完整的類(lèi)名即可。為了給按鈕一個(gè)點(diǎn)擊效果,你需要給他一個(gè)selector背景,這里就不說(shuō)了。
最后一步,即在activity中設(shè)置該控件的內(nèi)容。當(dāng)然,在xml中也可以設(shè)置,但是只能設(shè)置一個(gè),當(dāng)我們需要兩次使用這樣的控件,并且顯示內(nèi)容不同時(shí)就不行了。在activity中設(shè)置也非常簡(jiǎn)單,我們?cè)贑ustomButton這個(gè)類(lèi)中已經(jīng)寫(xiě)好了相應(yīng)的方法,簡(jiǎn)單調(diào)用即可。代碼如下:
package com.szy.customview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity { private CustomButton btnConfirm; private CustomButton btnCancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnConfirm = (CustomButton) findViewById(R.id.bt_confirm); btnCancel = (CustomButton) findViewById(R.id.bt_cancel); btnConfirm.setTextViewText("確定"); btnConfirm.setImageResource(R.drawable.confirm); btnCancel.setTextViewText("取消"); btnCancel.setImageResource(R.drawable.cancel); btnConfirm.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 在這里可以實(shí)現(xiàn)點(diǎn)擊事件 } }); } }
這樣,一個(gè)帶文字和圖片的組合按鈕控件就完成了。這樣梳理一下,使用還是非常簡(jiǎn)單的。組合控件能做的事還非常多,主要是在類(lèi)似上例中的CustomButton類(lèi)中寫(xiě)好要使用的方法即可。
再來(lái)看一個(gè)組合控件,帶刪除按鈕的EidtText。即在用戶輸入后,會(huì)出現(xiàn)刪除按鈕,點(diǎn)擊即可取消用戶輸入。
定義方法和上例一樣。首先寫(xiě)一個(gè)自定義控件的布局:
custom_editview.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:id="@+id/et" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" /> <ImageButton android:id="@+id/ib" android:visibility="gone" android:src="@drawable/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:layout_alignRight="@+id/et" /> </RelativeLayout>
實(shí)現(xiàn)輸入框右側(cè)帶按鈕效果,注意將按鈕隱藏。然后寫(xiě)一個(gè)CustomEditView類(lèi),實(shí)現(xiàn)刪除用戶輸入功能。這里用到了TextWatch這個(gè)接口,監(jiān)聽(tīng)輸入框中的文字變化。使用也很簡(jiǎn)單,實(shí)現(xiàn)他的三個(gè)方法即可??创a:
CustomEditView.java
package com.szy.customview; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; import android.widget.LinearLayout; public class CustomEditView extends LinearLayout implements EdtInterface { ImageButton ib; EditText et; public CustomEditView(Context context) { super(context); } public CustomEditView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.custom_editview, this, true); init(); } private void init() { ib = (ImageButton) findViewById(R.id.ib); et = (EditText) findViewById(R.id.et); et.addTextChangedListener(tw);// 為輸入框綁定一個(gè)監(jiān)聽(tīng)文字變化的監(jiān)聽(tīng)器 // 添加按鈕點(diǎn)擊事件 ib.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { hideBtn();// 隱藏按鈕 et.setText("");// 設(shè)置輸入框內(nèi)容為空 } }); } // 當(dāng)輸入框狀態(tài)改變時(shí),會(huì)調(diào)用相應(yīng)的方法 TextWatcher tw = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } // 在文字改變后調(diào)用 @Override public void afterTextChanged(Editable s) { if (s.length() == 0) { hideBtn();// 隱藏按鈕 } else { showBtn();// 顯示按鈕 } } }; @Override public void hideBtn() { // 設(shè)置按鈕不可見(jiàn) if (ib.isShown()) ib.setVisibility(View.GONE); } @Override public void showBtn() { // 設(shè)置按鈕可見(jiàn) if (!ib.isShown()) { ib.setVisibility(View.VISIBLE); } } } interface EdtInterface { public void hideBtn(); public void showBtn(); }
在TextWatch接口的afterTextChanged方法中對(duì)文字進(jìn)行判斷,若長(zhǎng)度為0,就隱藏按鈕,否則,顯示按鈕。
另外,實(shí)現(xiàn)ImageButton(即那個(gè)叉)的點(diǎn)擊事件,刪除輸入框中的內(nèi)容,并隱藏按鈕。
后面兩步的實(shí)現(xiàn)就是加入到實(shí)際布局中:
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.szy.customview.CustomEditView android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
最后顯示效果如圖:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Android控件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- android實(shí)現(xiàn)widget時(shí)鐘示例分享
- Android多功能時(shí)鐘開(kāi)發(fā)案例(實(shí)戰(zhàn)篇)
- Android多功能時(shí)鐘開(kāi)發(fā)案例(基礎(chǔ)篇)
- Android 仿日歷翻頁(yè)、仿htc時(shí)鐘翻頁(yè)、數(shù)字翻頁(yè)切換效果
- Android仿小米時(shí)鐘效果
- Android通過(guò)Path實(shí)現(xiàn)搜索按鈕和時(shí)鐘復(fù)雜效果
- Android實(shí)現(xiàn)簡(jiǎn)單時(shí)鐘View的方法
- android 自定義控件 自定義屬性詳細(xì)介紹
- android自定義控件和自定義回調(diào)函數(shù)步驟示例
- Android自定義控件實(shí)現(xiàn)可左右滑動(dòng)的導(dǎo)航條
- Android編程基于自定義控件實(shí)現(xiàn)時(shí)鐘功能的方法
相關(guān)文章
Android實(shí)現(xiàn)加載對(duì)話框
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)加載對(duì)話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01Android 編譯出錯(cuò)版本匹配問(wèn)題解決辦法
這篇文章主要介紹了Android 編譯出錯(cuò) app\build\intermediates\res\merged\debug\values-v23\values-v23.xml 的問(wèn)題解決辦法,需要的朋友可以參考下2017-07-07Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比
這篇文章主要介紹了 Android 自定義按鈕點(diǎn)擊事件和長(zhǎng)按事件對(duì)比的相關(guān)資料,需要的朋友可以參考下2017-04-04Android studio無(wú)法創(chuàng)建類(lèi)和接口和提示問(wèn)題的完美解決辦法
這篇文章主要介紹了Android studio無(wú)法創(chuàng)建類(lèi)和接口和提示問(wèn)題解決辦法,內(nèi)容比較簡(jiǎn)單,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04Kotlin Extension Function擴(kuò)展函數(shù)詳細(xì)介紹
Kotlin支持使用新功能擴(kuò)展類(lèi)的能力,而無(wú)需通過(guò)類(lèi)實(shí)現(xiàn)繼承概念或使用設(shè)計(jì)模式,如裝飾器(Decorator)。這是通過(guò)稱(chēng)為擴(kuò)展功能(Extension Function)的特殊方式來(lái)完成的。因此,此功能可以有效地使代碼變得更清晰和易于閱讀,并且還可以減少代碼2023-02-02flutter 自定義websocket路由的實(shí)現(xiàn)
這篇文章主要介紹了flutter 自定義websocket路由的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Android 實(shí)現(xiàn)定時(shí)任務(wù)的過(guò)程詳解
這篇文章主要介紹了Android 定時(shí)任務(wù)過(guò)程詳解的相關(guān)資料,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11詳解Android中的ActivityThread和APP啟動(dòng)過(guò)程
ActivityThread就是我們常說(shuō)的主線程或UI線程,ActivityThread的main方法是整個(gè)APP的入口,本篇深入學(xué)習(xí)下ActivityThread,順便了解下APP和Activity的啟動(dòng)過(guò)程。2021-06-06android?viewpager實(shí)現(xiàn)輪播效果
這篇文章主要為大家詳細(xì)介紹了android?viewpager實(shí)現(xiàn)輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06