Android信息界面編輯及組合控件的封裝

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

在山海關(guān)等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計制作、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作按需求定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)整合營銷推廣,外貿(mào)營銷網(wǎng)站建設(shè),山海關(guān)網(wǎng)站建設(shè)費(fèi)用合理。

Github地址(完整Demo,歡迎下載)

效果圖

Android信息界面編輯及組合控件的封裝

 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); //輸入框不允許長按
  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="請輸入姓名"
  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>

調(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();
   }
  });
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文標(biāo)題:Android信息界面編輯及組合控件的封裝
URL分享:http://muchs.cn/article28/jpisjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、微信小程序、云服務(wù)器企業(yè)網(wǎng)站制作、搜索引擎優(yōu)化、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)