Android信息界面編輯及組合控件的封裝
本文實(shí)例為大家分享了Android編輯信息界面,及組合控件的封裝,供大家參考,具體內(nèi)容如下
效果圖

attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ItemGroup"> <!--標(biāo)題的文字--> <attr name="title" format="string" /> <!--標(biāo)題的字體大小--> <attr name="title_size" format="dimension" /> <!--標(biāo)題的字體顏色--> <attr name="title_color" format="color" /> <!--輸入框的內(nèi)容--> <attr name="edt_content" format="string" /> <!--輸入框的字體大小--> <attr name="edt_text_size" format="dimension" /> <!--輸入框的字體顏色--> <attr name="edt_text_color" format="color" /> <!--輸入框提示的內(nèi)容--> <attr name="edt_hint_content" format="string" /> <!--輸入框的提示字體的字體顏色--> <attr name="edt_hint_text_color" format="color" /> <!--輸入框是否可以編輯內(nèi)容--> <attr name="isEditable" format="boolean"/> <!--向的右箭頭圖標(biāo)是否可見--> <attr name="jt_visible" format="boolean"/> <!--item布局的內(nèi)邊距--> <attr name="paddingLeft" format="dimension"/> <attr name="paddingRight" format="dimension"/> <attr name="paddingTop" format="dimension"/> <attr name="paddingBottom" format="dimension"/> <attr name="drawable_left" format="reference" /> <attr name="drawable_right" format="reference" /> <attr name="line_color" format="color" /> <attr name="line_height" format="integer" /> </declare-styleable> </resources>
獲取到各屬性
private void initAttrs(Context context, AttributeSet attrs) {
//標(biāo)題的默認(rèn)字體顏色
int defaultTitleColor = context.getResources().getColor(R.color.item_group_title);
//輸入框的默認(rèn)字體顏色
int defaultEdtColor = context.getResources().getColor(R.color.item_group_edt);
//輸入框的默認(rèn)的提示內(nèi)容的字體顏色
int defaultHintColor = context.getResources().getColor(R.color.item_group_edt);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemGroup);
String title = typedArray.getString(R.styleable.ItemGroup_title);
float paddingLeft = typedArray.getDimension(R.styleable.ItemGroup_paddingLeft, 15);
float paddingRight = typedArray.getDimension(R.styleable.ItemGroup_paddingRight, 15);
float paddingTop = typedArray.getDimension(R.styleable.ItemGroup_paddingTop, 5);
float paddingBottom = typedArray.getDimension(R.styleable.ItemGroup_paddingTop, 5);
float titleSize = typedArray.getDimension(R.styleable.ItemGroup_title_size, 15);
int titleColor = typedArray.getColor(R.styleable.ItemGroup_title_color, defaultTitleColor);
String content = typedArray.getString(R.styleable.ItemGroup_edt_content);
float contentSize = typedArray.getDimension(R.styleable.ItemGroup_edt_text_size, 13);
int contentColor = typedArray.getColor(R.styleable.ItemGroup_edt_text_color, defaultEdtColor);
String hintContent = typedArray.getString(R.styleable.ItemGroup_edt_hint_content);
int hintColor = typedArray.getColor(R.styleable.ItemGroup_edt_hint_text_color, defaultHintColor);
//默認(rèn)輸入框可以編輯
boolean isEditable = typedArray.getBoolean(R.styleable.ItemGroup_isEditable, true);
//向右的箭頭圖標(biāo)是否可見,默認(rèn)可見
boolean showJtIcon = typedArray.getBoolean(R.styleable.ItemGroup_jt_visible, true);
typedArray.recycle();
//設(shè)置數(shù)據(jù)
//設(shè)置item的內(nèi)邊距
itemGroupLayout.setPadding((int) paddingLeft, (int) paddingTop, (int) paddingRight, (int) paddingBottom);
titleTv.setText(title);
titleTv.setTextSize(titleSize);
titleTv.setTextColor(titleColor);
contentEdt.setText(content);
contentEdt.setTextSize(contentSize);
contentEdt.setTextColor(contentColor);
contentEdt.setHint(hintContent);
contentEdt.setHintTextColor(hintColor);
contentEdt.setFocusableInTouchMode(isEditable); //設(shè)置輸入框是否可以編輯
contentEdt.setLongClickable(false); //輸入框不允許長(zhǎng)按
jtRightIv.setVisibility(showJtIcon ? View.VISIBLE : View.GONE); //設(shè)置向右的箭頭圖標(biāo)是否可見
}
xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.zx.itemgroup.MainActivity"> <com.zx.itemgroup.ItemGroup android:id="@+id/name_ig" android:layout_width="match_parent" android:layout_height="wrap_content" app:edt_hint_content="請(qǐng)輸入姓名" app:jt_visible="false" app:paddingLeft="15dp" app:title="姓名" /> <com.zx.itemgroup.ItemGroup android:id="@+id/id_card_ig" android:layout_width="match_parent" android:layout_height="wrap_content" app:edt_hint_content="請(qǐng)輸入身份證號(hào)" app:jt_visible="false" app:paddingLeft="15dp" app:title="身份證" /> <com.zx.itemgroup.ItemGroup android:id="@+id/select_birthday_ig" android:layout_width="match_parent" android:layout_height="46dp" app:edt_hint_content="請(qǐng)選擇出生日期" app:isEditable="false" app:paddingLeft="15dp" app:title="出生日期" /> <com.zx.itemgroup.ItemGroup android:id="@+id/select_city_ig" android:layout_width="match_parent" android:layout_height="46dp" app:edt_hint_content="請(qǐng)選擇您所在的城市" app:isEditable="false" app:paddingLeft="15dp" app:title="所在城市" /> </LinearLayout>
調(diào)用的activity
/**
* 組合控件封裝(提交信息及編輯信息界面及功能)
*/
public class MainActivity extends AppCompatActivity {
private Context mContext;
private ItemGroup nameIG, idCardIG, birthdayIG, cityIG;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
initView();
}
private void initView() {
nameIG = (ItemGroup) findViewById(R.id.name_ig);
idCardIG = (ItemGroup) findViewById(R.id.id_card_ig);
birthdayIG = (ItemGroup) findViewById(R.id.select_birthday_ig);
cityIG = (ItemGroup) findViewById(R.id.select_city_ig);
birthdayIG.setItemOnClickListener(new ItemGroup.ItemOnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "點(diǎn)擊了選擇出生日期", Toast.LENGTH_SHORT).show();
}
});
cityIG.setItemOnClickListener(new ItemGroup.ItemOnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "點(diǎn)擊了選擇城市", Toast.LENGTH_SHORT).show();
}
});
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問題分析
這篇文章主要介紹了Android開發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問題,結(jié)合實(shí)例形式分析了Android三角函數(shù)運(yùn)算中的弧度與角度計(jì)算問題與相關(guān)解決方法,需要的朋友可以參考下2017-11-11
Android中Service和Activity相互通信示例代碼
在android中Activity負(fù)責(zé)前臺(tái)界面展示,service負(fù)責(zé)后臺(tái)的需要長(zhǎng)期運(yùn)行的任務(wù)。下面這篇文章主要給大家介紹了關(guān)于Android中Service和Activity相互通信的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
Android中DialogFragment自定義背景與寬高的方法
DialogFragment 彈出框默認(rèn)是在屏幕的中央,左右還有留白,那么如何自定義背景和寬高呢?下面這篇文章就來給大家介紹了關(guān)于Android中DialogFragment自定義背景與寬高的方法,需要的朋友可以參考下。2017-08-08
Android自定義UI手勢(shì)密碼改進(jìn)版源碼下載
這篇文章主要介紹了Android自定義UI手勢(shì)密碼改進(jìn)版,為大家提供了手勢(shì)密碼源碼下載,,具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下2016-10-10
Flutter?彈性布局基石flex算法flexible示例詳解
這篇文章主要為大家介紹了Flutter?彈性布局基石flex算法flexible示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android APK應(yīng)用安裝原理解析之AndroidManifest使用PackageParser.parserPac
這篇文章主要介紹了Android APK應(yīng)用安裝原理解析之AndroidManifest使用PackageParser.parserPackage原理,結(jié)合實(shí)例形式分析了PackageManagerService調(diào)用PackageParser.parserPackage方法解析APK清單相關(guān)原理與操作技巧,需要的朋友可以參考下2017-12-12
Android Google AutoService框架使用詳解
AutoService是Google開發(fā)一個(gè)自動(dòng)生成SPI清單文件的框架。看過一些基于APT的三方框架源碼的讀者應(yīng)該有所了解。比如Arouter、EventBus等等2022-11-11
Android獲取手機(jī)安裝應(yīng)用程序的詳細(xì)信息
在開發(fā)Android應(yīng)用時(shí),有時(shí)我們需要獲取設(shè)備上已安裝的所有應(yīng)用程序的信息,這篇文章為大家整理了一些常用的方法,希望對(duì)大家有所幫助2025-03-03
Android開發(fā)中總結(jié)的Adapter工具類【附完整源碼下載】
這篇文章主要介紹了Android開發(fā)中總結(jié)的Adapter工具類,簡(jiǎn)單說明了Adapter的功能,并結(jié)合實(shí)例形式分析了Adapter工具類的相關(guān)使用方法,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11
Android AutoCompleteTextView連接數(shù)據(jù)庫(kù)自動(dòng)提示的方法(附demo源碼下載)
這篇文章主要介紹了Android AutoCompleteTextView連接數(shù)據(jù)庫(kù)自動(dòng)提示的方法,結(jié)合實(shí)例形式分析了AutoCompleteTextView操作數(shù)據(jù)庫(kù)的原理與具體技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-02-02

