Android信息界面编辑及组合控件的封装
本文实例为大家分享了Android编辑信息界面,及组合控件的封装,供大家参考,具体内容如下
效果图

attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="ItemGroup"> <!--标题的文字--> <attr name="title" format="string" /> <!--标题的字体大小--> <attr name="title_size" format="dimension" /> <!--标题的字体颜色--> <attr name="title_color" format="color" /> <!--输入框的内容--> <attr name="edt_content" format="string" /> <!--输入框的字体大小--> <attr name="edt_text_size" format="dimension" /> <!--输入框的字体颜色--> <attr name="edt_text_color" format="color" /> <!--输入框提示的内容--> <attr name="edt_hint_content" format="string" /> <!--输入框的提示字体的字体颜色--> <attr name="edt_hint_text_color" format="color" /> <!--输入框是否可以编辑内容--> <attr name="isEditable" format="boolean"/> <!--向的右箭头图标是否可见--> <attr name="jt_visible" format="boolean"/> <!--item布局的内边距--> <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) {
//标题的默认字体颜色
int defaultTitleColor = context.getResources().getColor(R.color.item_group_title);
//输入框的默认字体颜色
int defaultEdtColor = context.getResources().getColor(R.color.item_group_edt);
//输入框的默认的提示内容的字体颜色
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);
//默认输入框可以编辑
boolean isEditable = typedArray.getBoolean(R.styleable.ItemGroup_isEditable, true);
//向右的箭头图标是否可见,默认可见
boolean showJtIcon = typedArray.getBoolean(R.styleable.ItemGroup_jt_visible, true);
typedArray.recycle();
//设置数据
//设置item的内边距
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); //设置输入框是否可以编辑
contentEdt.setLongClickable(false); //输入框不允许长按
jtRightIv.setVisibility(showJtIcon ? View.VISIBLE : View.GONE); //设置向右的箭头图标是否可见
}
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="请输入姓名" 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="请输入身份证号" 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="请选择出生日期" 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="请选择您所在的城市" app:isEditable="false" app:paddingLeft="15dp" app:title="所在城市" /> </LinearLayout>
调用的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, "点击了选择出生日期", Toast.LENGTH_SHORT).show();
}
});
cityIG.setItemOnClickListener(new ItemGroup.ItemOnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext, "点击了选择城市", Toast.LENGTH_SHORT).show();
}
});
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
Android开发中计算器的sin、cos及tan值计算问题分析
这篇文章主要介绍了Android开发中计算器的sin、cos及tan值计算问题,结合实例形式分析了Android三角函数运算中的弧度与角度计算问题与相关解决方法,需要的朋友可以参考下2017-11-11
Android中Service和Activity相互通信示例代码
在android中Activity负责前台界面展示,service负责后台的需要长期运行的任务。下面这篇文章主要给大家介绍了关于Android中Service和Activity相互通信的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。2017-09-09
Android中DialogFragment自定义背景与宽高的方法
DialogFragment 弹出框默认是在屏幕的中央,左右还有留白,那么如何自定义背景和宽高呢?下面这篇文章就来给大家介绍了关于Android中DialogFragment自定义背景与宽高的方法,需要的朋友可以参考下。2017-08-08
Flutter 弹性布局基石flex算法flexible示例详解
这篇文章主要为大家介绍了Flutter 弹性布局基石flex算法flexible示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-12-12
Android APK应用安装原理解析之AndroidManifest使用PackageParser.parserPac
这篇文章主要介绍了Android APK应用安装原理解析之AndroidManifest使用PackageParser.parserPackage原理,结合实例形式分析了PackageManagerService调用PackageParser.parserPackage方法解析APK清单相关原理与操作技巧,需要的朋友可以参考下2017-12-12
Android Google AutoService框架使用详解
AutoService是Google开发一个自动生成SPI清单文件的框架。看过一些基于APT的三方框架源码的读者应该有所了解。比如Arouter、EventBus等等2022-11-11
Android开发中总结的Adapter工具类【附完整源码下载】
这篇文章主要介绍了Android开发中总结的Adapter工具类,简单说明了Adapter的功能,并结合实例形式分析了Adapter工具类的相关使用方法,并附带完整源码供读者下载参考,需要的朋友可以参考下2017-11-11
Android AutoCompleteTextView连接数据库自动提示的方法(附demo源码下载)
这篇文章主要介绍了Android AutoCompleteTextView连接数据库自动提示的方法,结合实例形式分析了AutoCompleteTextView操作数据库的原理与具体技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下2016-02-02


最新评论