您的位置:首页 > 移动开发 > Android开发

Android编辑信息界面,组合控件的封装

2018-03-18 20:50 417 查看
Github地址(完整Demo,欢迎下载)

https://github.com/ganchuanpu/ItemGroup

效果图



attrs.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <declare-styleable name="ItemGroup">
4         <!--标题的文字-->
5         <attr name="title" format="string" />
6         <!--标题的字体大小-->
7         <attr name="title_size" format="dimension" />
8         <!--标题的字体颜色-->
9         <attr name="title_color" format="color" />
10         <!--输入框的内容-->
11         <attr name="edt_content" format="string" />
12         <!--输入框的字体大小-->
13         <attr name="edt_text_size" format="dimension" />
14         <!--输入框的字体颜色-->
15         <attr name="edt_text_color" format="color" />
16         <!--输入框提示的内容-->
17         <attr name="edt_hint_content" format="string" />
18         <!--输入框的提示字体的字体颜色-->
19         <attr name="edt_hint_text_color" format="color" />
20         <!--输入框是否可以编辑内容-->
21         <attr name="isEditable" format="boolean"/>
22         <!--向的右箭头图标是否可见-->
23         <attr name="jt_visible" format="boolean"/>
24         <!--item布局的内边距-->
25         <attr name="paddingLeft" format="dimension"/>
26         <attr name="paddingRight" format="dimension"/>
27         <attr name="paddingTop" format="dimension"/>
28         <attr name="paddingBottom" format="dimension"/>
29
30         <attr name="drawable_left" format="reference" />
31         <attr name="drawable_right" format="reference" />
32         <attr name="line_color" format="color" />
33         <attr name="line_height" format="integer" />
34     </declare-styleable>
35 </resources>


获取到各属性

1 private void initAttrs(Context context, AttributeSet attrs) {
2         //标题的默认字体颜色
3         int defaultTitleColor = context.getResources().getColor(R.color.item_group_title);
4         //输入框的默认字体颜色
5         int defaultEdtColor = context.getResources().getColor(R.color.item_group_edt);
6         //输入框的默认的提示内容的字体颜色
7         int defaultHintColor = context.getResources().getColor(R.color.item_group_edt);
8
9         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemGroup);
10         String title = typedArray.getString(R.styleable.ItemGroup_title);
11         float paddingLeft = typedArray.getDimension(R.styleable.ItemGroup_paddingLeft, 15);
12         float paddingRight = typedArray.getDimension(R.styleable.ItemGroup_paddingRight, 15);
13         float paddingTop = typedArray.getDimension(R.styleable.ItemGroup_paddingTop, 5);
14         float paddingBottom = typedArray.getDimension(R.styleable.ItemGroup_paddingTop, 5);
15         float titleSize = typedArray.getDimension(R.styleable.ItemGroup_title_size, 15);
16         int titleColor = typedArray.getColor(R.styleable.ItemGroup_title_color, defaultTitleColor);
17         String content = typedArray.getString(R.styleable.ItemGroup_edt_content);
18         float contentSize = typedArray.getDimension(R.styleable.ItemGroup_edt_text_size, 13);
19         int contentColor = typedArray.getColor(R.styleable.ItemGroup_edt_text_color, defaultEdtColor);
20         String hintContent = typedArray.getString(R.styleable.ItemGroup_edt_hint_content);
21         int hintColor = typedArray.getColor(R.styleable.ItemGroup_edt_hint_text_color, defaultHintColor);
22         //默认输入框可以编辑
23         boolean isEditable = typedArray.getBoolean(R.styleable.ItemGroup_isEditable, true);
24         //向右的箭头图标是否可见,默认可见
25         boolean showJtIcon = typedArray.getBoolean(R.styleable.ItemGroup_jt_visible, true);
26         typedArray.recycle();
27
28         //设置数据
29         //设置item的内边距
30         itemGroupLayout.setPadding((int) paddingLeft, (int) paddingTop, (int) paddingRight, (int) paddingBottom);
31         titleTv.setText(title);
32         titleTv.setTextSize(titleSize);
33         titleTv.setTextColor(titleColor);
34
35         contentEdt.setText(content);
36         contentEdt.setTextSize(contentSize);
37         contentEdt.setTextColor(contentColor);
38         contentEdt.setHint(hintContent);
39         contentEdt.setHintTextColor(hintColor);
40         contentEdt.setFocusableInTouchMode(isEditable); //设置输入框是否可以编辑
41         contentEdt.setLongClickable(false); //输入框不允许长按
42         jtRightIv.setVisibility(showJtIcon ? View.VISIBLE : View.GONE);  //设置向右的箭头图标是否可见
43 }


xml布局文件

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3     xmlns:app="http://schemas.android.com/apk/res-auto"
4     xmlns:tools="http://schemas.android.com/tools"
5     android:layout_width="match_parent"
6     android:layout_height="match_parent"
7     android:orientation="vertical"
8     tools:context="com.zx.itemgroup.MainActivity">
9
10     <com.zx.itemgroup.ItemGroup
11         android:id="@+id/name_ig"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         app:edt_hint_content="请输入姓名"
15         app:jt_visible="false"
16         app:paddingLeft="15dp"
17         app:title="姓名" />
18
19     <com.zx.itemgroup.ItemGroup
20         android:id="@+id/id_card_ig"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         app:edt_hint_content="请输入身份证号"
24         app:jt_visible="false"
25         app:paddingLeft="15dp"
26         app:title="身份证" />
27
28     <com.zx.itemgroup.ItemGroup
29         android:id="@+id/select_birthday_ig"
30         android:layout_width="match_parent"
31         android:layout_height="46dp"
32         app:edt_hint_content="请选择出生日期"
33         app:isEditable="false"
34         app:paddingLeft="15dp"
35         app:title="出生日期" />
36
37     <com.zx.itemgroup.ItemGroup
38         android:id="@+id/select_city_ig"
39         android:layout_width="match_parent"
40         android:layout_height="46dp"
41         app:edt_hint_content="请选择您所在的城市"
42         app:isEditable="false"
43         app:paddingLeft="15dp"
44         app:title="所在城市" />
45 </LinearLayout>


调用的activity

1 /**
2  * 组合控件封装(提交信息及编辑信息界面及功能)
3  */
4 public class MainActivity extends AppCompatActivity {
5
6     private Context mContext;
7     private ItemGroup nameIG, idCardIG, birthdayIG, cityIG;
8
9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13
14         mContext = this;
15         initView();
16     }
17
18     private void initView() {
19         nameIG = (ItemGroup) findViewById(R.id.name_ig);
20         idCardIG = (ItemGroup) findViewById(R.id.id_card_ig);
21         birthdayIG = (ItemGroup) findViewById(R.id.select_birthday_ig);
22         cityIG = (ItemGroup) findViewById(R.id.select_city_ig);
23         birthdayIG.setItemOnClickListener(new ItemGroup.ItemOnClickListener() {
24             @Override
25             public void onClick(View v) {
26                 Toast.makeText(mContext, "点击了选择出生日期", Toast.LENGTH_SHORT).show();
27             }
28         });
29         cityIG.setItemOnClickListener(new ItemGroup.ItemOnClickListener() {
30             @Override
31             public void onClick(View v) {
32                 Toast.makeText(mContext, "点击了选择城市", Toast.LENGTH_SHORT).show();
33             }
34         });
35     }
36 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐