不知道各位小伙伴在开发应用客户端的时候有没有碰到过这样的需求,就是需要在app端维护大量的表数据。
一般的应用可能需要客户端上传数据的时候 就几张表或者几个功能,但是大量的表格就很复杂。
举列:我们碰到的需求是 维护表格数据 ,这些表格都是政府部门的一些表格文件,而且非常多,一个轨道系统有将近400张表格。对 是400多张,而且没有任何规律,字段什么的都不一样,在之前有个房建系统做了大概几十张,之前的小伙伴是写了几十个activity... 但是这次400张怎么处理?
如图下面的表格:
类似上面的表格,400多个,都要可以实现在app端填写,然后上传到服务器。
app端最后实现效果(手机比较小,截图搞了很多张,gif做出来都太大,传不上来,大概意思能看)
界面不美观,凑合看吧...说重点..
实现的整体思路:
1.处理 400张表的数据
将400张表的字段存到一张表里面,整理这400张表的数据是个比较大的工作,我们有个做delphi的工程师专门做了一个配置工具,打开每张表配置这张表格的字段。
数据库主要包括4张表格:T_Table 存储所有表格数据 T_TableDetail 存储所有表的字段数据 T_Type 字典表 存储所有控件类型 T_DIC存储所有表下拉数据
T_Table 字段包括:id name order parentID(因为我们这个表显示有层级结构)
T_TableDetail字段主要包括 :字段id 字段名称 字段顺序 表格id(属于哪张表) 类型id(这个字段要用什么控件显示) isRequired 是否必填 defaultValue默认值
row col 行列 dicid关联字典表数据
核心的就这些 这个根据自己的具体业务再搭建自己的数据库表结构。
2.搭建界面
有了基础数据后,我们要做的就是写一个通用Activity去动态拼装整个界面
我实现的思路是这样的 :将每种类型的控件都单独写成xml,比如有15种类型的控件就写15个xml,根据字段类型判断加载哪种类型的xml。
判断加载完界面 处理界面控件设置默认值,设置点击事件,设置是否可以编辑点击等(因为查看 修改 添加都用一套界面),这一些基本逻辑。
我就直接将初始化布局的地方贴出来,大家看一下。
public class ViewTypeUtil { //表格名称类型 输入框 默认带值 且要单独获取值 public static final int item_view_type_zero = 0; //单行输入框 public static final int item_view_type_one = 1; //多行输入框 public static final int item_view_type_two = 2; //短日期 如2012-01-12 public static final int item_view_type_three = 3; //数值 public static final int item_view_type_four = 4; //下拉框 public static final int item_view_type_five = 5; //分部分项 public static final int item_view_type_six = 6; //部位栏 public static final int item_view_type_seven = 7; //单选框 public static final int item_view_type_eight = 8; //多选框 public static final int item_view_type_nine= 9; //列表 public static final int item_view_type_ten = 10; //图纸标准 public static final int item_view_type_eleven = 11; //图片 public static final int item_view_type_twelve = 12; //组 public static final int item_view_type_thirteen = 13; //长日期 2017-07-02 10:38:41 public static final int item_view_type_fourteen = 14; //时间格式 如 10:25:14 public static final int item_view_type_fifteen = 15; //标签 public static final int item_view_type_sixteen = 16; }
public void initView(final LinearLayout linearLayout, List<FormDetail> formDetailList) { for (int i = 0; i < formDetailList.size(); i++) { final FormDetail formDetail = formDetailList.get(i); final int mType = formDetail.getTypeId(); if (mType == ViewTypeUtil.item_view_type_zero) { mTypeZeroView = LayoutInflater.from(this).inflate(R.layout.item_type_zero, null); mItemZeroFieldTv = (TextView) mTypeZeroView.findViewById(R.id.item_zero_field_tv); mItemZeroValueEt = (EditText) mTypeZeroView.findViewById(R.id.item_zero_value_et); if (mIntentFrom == FLAG_SHOW) { mItemZeroValueEt.setEnabled(false); } mItemZeroValueEt.setText(formDetail.getuValue()); mItemZeroValueEt.setTag(formDetail.getTableDetailID()); mItemZeroFieldTv.setText(formDetail.getCaption()); mTypeZeroView.setTag(mType); linearLayout.addView(mTypeZeroView); } else if (mType == ViewTypeUtil.item_view_type_one) { mTypeOneView = LayoutInflater.from(this).inflate(R.layout.item_type_one, null); mItemOneFieldTv = (TextView) mTypeOneView.findViewById(R.id.item_one_field_tv); mItemOneValueEt = (EditText) mTypeOneView.findViewById(R.id.item_one_value_et); if (mIntentFrom == FLAG_SHOW) { mItemOneValueEt.setEnabled(false); } mItemOneValueEt.setText(formDetail.getuValue()); mItemOneValueEt.setTag(formDetail.getTableDetailID()); mItemOneFieldTv.setText(formDetail.getCaption()); mTypeOneView.setTag(mType); linearLayout.addView(mTypeOneView); } else if (mType == ViewTypeUtil.item_view_type_two) { mTypeTwoView = LayoutInflater.from(this).inflate(R.layout.item_type_two, null); mItemTwoValueEt = (EditText) mTypeTwoView.findViewById(R.id.content_et); mItemTwoValueEt.setTypeface(Constants.fontFace); mItemTwoFieldTv = (TextView) mTypeTwoView.findViewById(R.id.item_two_field_tv); if (mIntentFrom == FLAG_SHOW) { mItemTwoValueEt.setEnabled(false); } else { mTypeTwoView.findViewById(R.id.content_iv).setOnClickListener(this); mTypeTwoView.findViewById(R.id.voice_iv).setOnClickListener(this); mTypeTwoView.findViewById(R.id.special_iv).setOnClickListener(this); } mItemTwoValueEt.setText(formDetail.getuValue()); mItemTwoFieldTv.setText(formDetail.getCaption()); mItemTwoValueEt.setTag(formDetail.getTableDetailID()); mTypeTwoView.findViewById(R.id.voice_iv).setTag(formDetail.getTableDetailID()); mTypeTwoView.findViewById(R.id.special_iv).setTag(formDetail.getTableDetailID()); mTypeTwoView.findViewById(R.id.content_iv).setTag(formDetail.getTableDetailID()); mTypeTwoView.setTag(mType); linearLayout.addView(mTypeTwoView); } else if (mType == ViewTypeUtil.item_view_type_three) { mTypeThreeView = LayoutInflater.from(this).inflate(R.layout.item_type_three, null); mDateKeyTv = (TextView) mTypeThreeView.findViewById(R.id.tv_test_date_key); mDateValueEt = (EditText) mTypeThreeView.findViewById(R.id.et_test_date_value); mDateKeyTv.setText(formDetail.getCaption()); if (mIntentFrom == FLAG_SHOW) { mDateValueEt.setEnabled(false); } else { mDateValueEt.setOnClickListener(this); } if (!TextUtils.isEmpty(formDetail.getuValue())) { mDateValueEt.setText(formDetail.getuValue()); } else { mDateValueEt.setText(TimeUtil.getCurDateStr().substring(0, 12)); } mDateValueEt.setTag(formDetail.getTableDetailID()); mTypeThreeView.setTag(mType); linearLayout.addView(mTypeThreeView); } else if (mType == ViewTypeUtil.item_view_type_four) { mTypeFourView = LayoutInflater.from(context).inflate(R.layout.item_type_four, null); mNumEt = (EditText) mTypeFourView.findViewById(R.id.et_num); mItemFourFieldTv = (TextView) mTypeFourView.findViewById(R.id.item_four_field_tv); mNumEt.setText(formDetail.getuValue()); mNumEt.setTag(formDetail.getTableDetailID()); mItemFourFieldTv.setText(formDetail.getCaption()); if (mIntentFrom == FLAG_SHOW) { mNumEt.setEnabled(false); } mTypeFourView.setTag(mType); linearLayout.addView(mTypeFourView); } else if (mType == ViewTypeUtil.item_view_type_five) { mTypeFiveView = LayoutInflater.from(this).inflate(R.layout.item_type_five, null); mSelectKeyTv = (TextView) mTypeFiveView.findViewById(R.id.et_select_key); mSelectValueEt = (EditText) mTypeFiveView.findViewById(R.id.et_select_value); mSelectValueEt.setText(formDetail.getuValue()); if (mIntentFrom == FLAG_SHOW) { mSelectValueEt.setEnabled(false); } else { mSelectValueEt.setOnClickListener(this); } mSelectKeyTv.setText(formDetail.getCaption()); mSelectValueEt.setTag(R.id.select_view_position, i); mSelectValueEt.setTag(R.id.select_view_detial_id, formDetail.getTableDetailID()); mTypeFiveView.setTag(mType); linearLayout.addView(mTypeFiveView); } else if (mType == ViewTypeUtil.item_view_type_six) { } else if (mType == ViewTypeUtil.item_view_type_seven) {// 部位栏 mTypeSevenView = LayoutInflater.from(this).inflate(R.layout.item_type_seven, null); mPartSelectKeyTv = (TextView) mTypeSevenView.findViewById(R.id.tv_select_part_key); mPartSelectValueEt = (EditText) mTypeSevenView.findViewById(R.id.et_select_part_value); mPartSelectKeyTv.setText(formDetail.getCaption()); mPartSelectValueEt.setText(formDetail.getuValue()); if (mIntentFrom == FLAG_SHOW) { mPartSelectValueEt.setEnabled(false); } else { mPartSelectValueEt.setOnClickListener(this); } mPartSelectValueEt.setTag(formDetail.getTableDetailID()); mTypeSevenView.setTag(mType); linearLayout.addView(mTypeSevenView); } else if (mType == ViewTypeUtil.item_view_type_eight) {// 单选框 mTypeEightView = LayoutInflater.from(this).inflate(R.layout.item_type_eight, null); mTypeEightKeyTv = (TextView) mTypeEightView.findViewById(R.id.tv_type_eight_key); rgTypeEight = (RadioGroup) mTypeEightView.findViewById(R.id.rb_type_eight); mTypeEightKeyTv.setText(formDetail.getCaption()); List<DicBean> dicBeans = formDetail.getDicBeanList(); if (dicBeans == null) { return; } for (int j = 0; j < dicBeans.size(); j++) { RadioButton radioButton = new RadioButton(context); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); radioButton.setText(dicBeans.get(j).getDicName()); radioButton.setTag(formDetail.getTableDetailID() + "," + dicBeans.get(j).getDicID()); radioButton.setId(j); rgTypeEight.addView(radioButton, params); if (mIntentFrom == FLAG_SHOW) {// 查看 radioButton.setEnabled(false); if ((dicBeans.get(j).getDicID() + "").equals(formDetail.getuValue())) { rgTypeEight.check(j); } } else if (mIntentFrom == FLAG_UPDATE) {// 修改 radioButton.setEnabled(true); if ((dicBeans.get(j).getDicID() + "").equals(formDetail.getuValue())) { rgTypeEight.check(j); } } else {// 添加 rgTypeEight.check(0); radioButton.setEnabled(true); } } mTypeEightView.setTag(mType); linearLayout.addView(mTypeEightView); } else if (mType == ViewTypeUtil.item_view_type_nine) {// 多选框 mTypeNineView = LayoutInflater.from(this).inflate(R.layout.item_type_nine, null); mTypeNineKeyTv = (TextView) mTypeNineView.findViewById(R.id.tv_type_nine_key);// 没有用到 cbxTypeNine = (CheckBox) mTypeNineView.findViewById(R.id.cbx_type_nine); cbxTypeNine.setText(formDetail.getCaption()); cbxTypeNine.setTag(formDetail.getTableDetailID() + "," + formDetail.getDicID()); cbxTypeNine.setOnClickListener(this); mTypeNineView.setTag(mType); linearLayout.addView(mTypeNineView); } else if (mType == ViewTypeUtil.item_view_type_ten) {// 列表 final String detailId = formDetail.getTableDetailID(); mTypeTenView = LayoutInflater.from(context).inflate(R.layout.item_type_ten, null); mTypeTenKeyTv = (TextView) mTypeTenView.findViewById(R.id.tv_type_ten_key); mTypeTenKeyTv.setText(formDetail.getCaption()); mTypeTenLinearLayout = (LinearLayout) mTypeTenView.findViewById(R.id.ly_type_ten); mTypeTenLinearLayout.setTag(formDetail.getTableDetailID()); mTypeTenView.setTag(mType); linearLayout.addView(mTypeTenView); new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { if (mIntentFrom == FLAG_SHOW || mIntentFrom == FLAG_UPDATE) {// 查看,修改 mFormDetailMap = GdjtUserDataDBOperation.getInstance().getChildListDetailValueData(mUTableID, formDetail.getTableDetailID()); } else {// 添加 mFormDetailChildList = GdjtFormDBOperation.getInstance().getChildListDetailData(formDetail.getTableDetailID()); } return null; } @Override protected void onPostExecute(Void result) { if (mIntentFrom == FLAG_SHOW || mIntentFrom == FLAG_UPDATE) { mTypeTenView.findViewById(R.id.iv_type_ten_add).setVisibility(View.GONE); mTypeTenLinearLayout.setWeightSum(3.0f); ShowChildView(); } else { AddListChildView(); mTypeTenLinearLayout.setWeightSum(2.5f); mTypeTenView.findViewById(R.id.iv_type_ten_add).setVisibility(View.VISIBLE); mTypeTenView.findViewById(R.id.iv_type_ten_add).setOnClickListener(CommonFormViewActivity.this); } } }.execute(); } else if (mType == ViewTypeUtil.item_view_type_eleven) {// 图纸标准 mTypeElevenView = LayoutInflater.from(this).inflate(R.layout.item_type_eleven, null); mTypeElevenKeyTv = (TextView) mTypeElevenView.findViewById(R.id.tv_type_eleven_key); mDrawingNameTv = (TextView) mTypeElevenView.findViewById(R.id.item_eleven_value_et); mDrawingNameTv.setOnClickListener(this); mDrawingNameTv.setTag(formDetail.getTableDetailID()); if (!TextUtils.isEmpty(mDrawingPath)) { mDrawingNameTv.setText(mDrawingPath.subSequence(mDrawingPath.lastIndexOf("/") + 1, mDrawingPath.length())); } mTypeElevenView.findViewById(R.id.item_eleven_value_img_photo).setOnClickListener(this); mTypeElevenView.findViewById(R.id.item_eleven_value_img_folder).setOnClickListener(this); mTypeElevenKeyTv.setText(formDetail.getCaption()); mTypeElevenView.setTag(mType); linearLayout.addView(mTypeElevenView); } else if (mType == ViewTypeUtil.item_view_type_twelve) {// 图片 mTypeTwelveView = LayoutInflater.from(this).inflate(R.layout.item_type_twelve, null); mTypeTwelveKeyTv = (TextView) mTypeTwelveView.findViewById(R.id.tv_type_twelve_key); mTypeTwelveValueTv = (TextView) mTypeTwelveView.findViewById(R.id.item_twelve_value_path); mTypeTwelveTakePhoto = mTypeTwelveView.findViewById(R.id.item_twelve_value_img_photo); mTypeTwelveSelectPhoto = mTypeTwelveView.findViewById(R.id.item_twelve_value_img_folder); if (mIntentFrom == FLAG_SHOW) { mTypeTwelveTakePhoto.setEnabled(false); mTypeTwelveSelectPhoto.setEnabled(false); } else { mTypeTwelveTakePhoto.setOnClickListener(this); mTypeTwelveSelectPhoto.setOnClickListener(this); } mTypeTwelveValueTv.setOnClickListener(this); mPhotoPath = formDetail.getuValue(); mTypeTwelveKeyTv.setText(formDetail.getCaption()); mTypeTwelveValueTv.setText(mPhotoPath.substring(mPhotoPath.lastIndexOf("/") + 1)); mTypeTwelveValueTv.setTag(formDetail.getTableDetailID()); mTypeTwelveTakePhoto.setTag(formDetail.getTableDetailID()); mTypeTwelveSelectPhoto.setTag(formDetail.getTableDetailID()); mTypeTwelveView.setTag(mType); linearLayout.addView(mTypeTwelveView); } else if (mType == ViewTypeUtil.item_view_type_thirteen) {// 组 final String detailId = formDetail.getTableDetailID(); mTypeThirteenView = LayoutInflater.from(context).inflate(R.layout.item_type_thirteen, null); mTypeThirteenKeyTv = (TextView) mTypeThirteenView.findViewById(R.id.tv_type_thirteen_key); mTypeThirteenKeyTv.setText(formDetail.getCaption()); mTypeThirteenLinearLayout = (LinearLayout) mTypeThirteenView.findViewById(R.id.ly_type_thirteen); mTypeThirteenLinearLayout.setTag(detailId); mTypeThirteenView.setTag(mType); linearLayout.addView(mTypeThirteenView); new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { if (mIntentFrom == FLAG_SHOW || mIntentFrom == FLAG_UPDATE) {// 查看,修改 mFormDetailMap = GdjtUserDataDBOperation.getInstance().getChildListDetailValueData(mUTableID, detailId); } else {// 添加 mFormZuDetailChildList = GdjtFormDBOperation.getInstance().getChildListDetailData(detailId); } return null; } @Override protected void onPostExecute(Void result) { if (mIntentFrom == FLAG_SHOW || mIntentFrom == FLAG_UPDATE ) { ShowChildView(); } else { AddZuChildView(); } // linearLayout.refreshDrawableState(); } }.execute(); } else if (mType == ViewTypeUtil.item_view_type_fourteen) {// 日期时间格式 mTypeFourteenView = LayoutInflater.from(this).inflate(R.layout.item_type_three, null); mFourteenDateKeyTv = (TextView) mTypeFourteenView.findViewById(R.id.tv_test_date_key); mFourteenDateValueEt = (EditText) mTypeFourteenView.findViewById(R.id.et_test_date_value); mFourteenDateValueEt.setTag(formDetail.getTableDetailID()); mFourteenDateKeyTv.setText(formDetail.getCaption()); if (mIntentFrom == FLAG_SHOW) { mFourteenDateValueEt.setEnabled(false); } if (!TextUtils.isEmpty(formDetail.getuValue())) { mFourteenDateValueEt.setText(formDetail.getuValue()); } else { mFourteenDateValueEt.setText(TimeUtil.getCurDateStr()); } mFourteenDateValueEt.setTag(formDetail.getTableDetailID()); mFourteenDateValueEt.setOnClickListener(this); mTypeFourteenView.setTag(mType); linearLayout.addView(mTypeFourteenView); } else if (mType == ViewTypeUtil.item_view_type_fifteen) {// 时间格式 mTypeFifteenView = LayoutInflater.from(this).inflate(R.layout.item_type_three, null); mFifteenDateKeyTv = (TextView) mTypeFifteenView.findViewById(R.id.tv_test_date_key); mFifteenDateValueEt = (EditText) mTypeFifteenView.findViewById(R.id.et_test_date_value); mFifteenDateValueEt.setTag(formDetail.getTableDetailID()); mFifteenDateKeyTv.setText(formDetail.getCaption()); if (mIntentFrom == FLAG_SHOW) { mFifteenDateValueEt.setEnabled(false); } if (!TextUtils.isEmpty(formDetail.getuValue())) { mFifteenDateValueEt.setText(formDetail.getuValue()); } else { mFifteenDateValueEt.setText(TimeUtil.getStringTimeShort()); } mFifteenDateValueEt.setTag(formDetail.getTableDetailID()); mFifteenDateValueEt.setOnClickListener(this); mTypeFifteenView.setTag(mType); linearLayout.addView(mTypeFifteenView); } else if (mType == ViewTypeUtil.item_view_type_sixteen) {// 标签 mTypeSixteenView = LayoutInflater.from(this).inflate(R.layout.item_type_zero, null); mItemSixteenFieldTv = (TextView) mTypeSixteenView.findViewById(R.id.item_zero_field_tv); mItemSixteenValueEt = (EditText) mTypeSixteenView.findViewById(R.id.item_zero_value_et); mItemSixteenValueEt.setEnabled(false); mItemSixteenValueEt.setText(formDetail.getuValue()); mItemSixteenValueEt.setTag(formDetail.getTableDetailID()); mItemSixteenFieldTv.setText(formDetail.getCaption()); mTypeSixteenView.setTag(mType); linearLayout.addView(mTypeSixteenView); } } }
3.处理复杂控件
其实这个里面的复杂控件就两种,一种是列表 一种是组
给简单大家解释一下列表是什么意思: 大家看有的表格里面是多行多列的,意味着同样的字段数据 可能要添写多条。
那么对应在app里面 就是 这些字段放到了一起 做成了一个 组。 那么这些字段添加多条 就是 在组后面放了一个 + 号
点击+号 这个组 就增加一组 。所以这个带+号的字段 就是 列表字段。
组和 列表 处理起来相对来说非常麻烦。
1).子项数据获取
2).布局嵌套
3).点击事件单独处理
4)保存数据独立拼接
具体这些问题,我就不一一展开说了。大家倘若有做的,具体做的时候自然会体会到。
4.处理点击事件
界面拼接完成后 ,要处理控件里面的点击事件。做的时候就会发现 倘若界面出现同一个item布局 ,布局里面包含点击事件 ,正常给控件设置点击事件,会发现 点击控件的时候无法区分开每个点击事件
所以处理点击事件的时候:原理 就是先循环整体的大布局 ,找到里面的每一项小布局,然后单独处理每一项小布局里面的控件。
这样才能保证点击事件没有问题。
代码贴一下:
@Override public void onClick(View v) { if (v.getId() == R.id.content_iv) {// 范例 } else if (v.getId() == R.id.voice_iv) {// 语音 for (int i = 0; i < mMainView.getChildCount(); i++) { int viewTag = (int) mMainView.getChildAt(i).getTag(); if (viewTag == ViewTypeUtil.item_view_type_two) { LinearLayout l = (LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0); EditText editText = (EditText) l.getChildAt(1); if (v.getTag().equals(editText.getTag())) { mVoice = new VoiceToWord(this, editText); mVoice.GetWordFromVoice(); } } else if (viewTag == ViewTypeUtil.item_view_type_thirteen || viewTag == ViewTypeUtil.item_view_type_ten) { LinearLayout l = (LinearLayout) ((LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0)).getChildAt(1); for (int j = 0; j < l.getChildCount(); j++) { LinearLayout l_add = (LinearLayout) ((LinearLayout) l.getChildAt(j)); for (int k = 0; k < l_add.getChildCount(); k++) { int viewTag_child = (int) l_add.getChildAt(k).getTag(); if (viewTag_child == ViewTypeUtil.item_view_type_two) { LinearLayout l_view = (LinearLayout) ((LinearLayout) l_add.getChildAt(k)).getChildAt(0); EditText editText = (EditText) l_view.getChildAt(1); if (v.getTag().equals(editText.getTag())) { mVoice = new VoiceToWord(this, editText); mVoice.GetWordFromVoice(); } } } } } } } else if (v.getId() == R.id.special_iv) {// 特殊字符 for (int i = 0; i < mMainView.getChildCount(); i++) { int viewTag = (int) mMainView.getChildAt(i).getTag(); if (viewTag == ViewTypeUtil.item_view_type_two) { LinearLayout l = (LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0); EditText editText = (EditText) l.getChildAt(1); if (v.getTag().equals(editText.getTag())) { mSpecialdialog = new SpecialWordsDialog(context, editText); } } else if (viewTag == ViewTypeUtil.item_view_type_thirteen || viewTag == ViewTypeUtil.item_view_type_ten) { LinearLayout l = (LinearLayout) ((LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0)).getChildAt(1); for (int j = 0; j < l.getChildCount(); j++) { LinearLayout l_add = (LinearLayout) ((LinearLayout) l.getChildAt(j)); for (int k = 0; k < l_add.getChildCount(); k++) { int viewTag_child = (int) l_add.getChildAt(k).getTag(); if (viewTag_child == ViewTypeUtil.item_view_type_two) { LinearLayout l_view = (LinearLayout) ((LinearLayout) l_add.getChildAt(k)).getChildAt(0); EditText editText = (EditText) l_view.getChildAt(1); if (v.getTag().equals(editText.getTag())) { mSpecialdialog = new SpecialWordsDialog(context, editText); } } } } } } } else if (v.getId() == R.id.et_test_date_value) {// 时间选择 for (int i = 0; i < mMainView.getChildCount(); i++) { int viewTag = (int) mMainView.getChildAt(i).getTag(); if (viewTag == ViewTypeUtil.item_view_type_three) { LinearLayout l = (LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0); EditText editText = (EditText) l.getChildAt(1); if (v.getTag().equals(editText.getTag())) { DlgUtil.showDateTimePickerDlgByType(this, editText, 1); } } else if (viewTag == ViewTypeUtil.item_view_type_fourteen) { LinearLayout l = (LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0); EditText editText = (EditText) l.getChildAt(1); if (v.getTag().equals(editText.getTag())) { DlgUtil.showDateTimePickerDlg(this, editText); } } else if (viewTag == ViewTypeUtil.item_view_type_fifteen) { LinearLayout l = (LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0); EditText editText = (EditText) l.getChildAt(1); if (v.getTag().equals(editText.getTag())) { DlgUtil.showDateTimePickerDlgByType(this, editText, 2); } } else if (viewTag == ViewTypeUtil.item_view_type_thirteen || viewTag == ViewTypeUtil.item_view_type_ten) { LinearLayout l = (LinearLayout) ((LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0)).getChildAt(1); for (int j = 0; j < l.getChildCount(); j++) { LinearLayout l_add = (LinearLayout) ((LinearLayout) l.getChildAt(j)); for (int k = 0; k < l_add.getChildCount(); k++) { int viewTag_child = (int) l_add.getChildAt(k).getTag(); if (viewTag_child == ViewTypeUtil.item_view_type_three) { LinearLayout l_view = (LinearLayout) ((LinearLayout) l_add.getChildAt(i)).getChildAt(0); EditText editText = (EditText) l_view.getChildAt(1); if (v.getTag().equals(editText.getTag())) { DlgUtil.showDateTimePickerDlgByType(this, editText, 1); } } else if (viewTag_child == ViewTypeUtil.item_view_type_fourteen) { LinearLayout l_view = (LinearLayout) ((LinearLayout) l_add.getChildAt(i)).getChildAt(0); EditText editText = (EditText) l_view.getChildAt(1); if (v.getTag().equals(editText.getTag())) { DlgUtil.showDateTimePickerDlg(this, editText); } } else if (viewTag_child == ViewTypeUtil.item_view_type_fifteen) { LinearLayout l_view = (LinearLayout) ((LinearLayout) l_add.getChildAt(i)).getChildAt(0); EditText editText = (EditText) l_view.getChildAt(1); if (v.getTag().equals(editText.getTag())) { DlgUtil.showDateTimePickerDlgByType(this, editText, 2); } } } } } } } else if (v.getId() == R.id.et_select_value) { for (int i = 0; i < mMainView.getChildCount(); i++) { int viewTag = (int) mMainView.getChildAt(i).getTag(); if (viewTag == ViewTypeUtil.item_view_type_five) { LinearLayout l = (LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0); final EditText editText = (EditText) l.getChildAt(1); if (v.getTag(R.id.select_view_detial_id).equals(editText.getTag(R.id.select_view_detial_id))) { int position = (int) editText.getTag(R.id.select_view_position); mDataList.clear(); List<DicBean> dicList = mFormDetailList.get(position).dicBeanList; for (DicBean dicBean : dicList) { mDataList.add(dicBean.getDicName()); } CusSpinerAdapter<String> mAdapter = new CusSpinerAdapter<String>(this); mAdapter.refreshData(mDataList, 0); View contentView = LayoutInflater.from(context).inflate(R.layout.common_form_view, null); SpinerPopWindow mSpinerPopWindow = new SpinerPopWindow(this, R.layout.spiner_window_layout, true); mSpinerPopWindow.setAdatper(mAdapter); mSpinerPopWindow.setItemListener(new IOnItemSelectListener() { @Override public void onItemClick(int pos) { if (pos >= 0 && pos <= mDataList.size()) { String value = (String) mDataList.get(pos); editText.setText(value.toString()); } } }); mSpinerPopWindow.setWidth(editText.getWidth()); int windowPos[] = PopMeasure.calculatePopWindowPos(editText, contentView); int xOff = 25;// 可以自己调整偏移 windowPos[0] -= xOff; mSpinerPopWindow.showAtLocation(v, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]); } }else if(viewTag == ViewTypeUtil.item_view_type_thirteen || viewTag == ViewTypeUtil.item_view_type_ten){ LinearLayout l = (LinearLayout) ((LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0)).getChildAt(1); for (int j = 0; j < l.getChildCount(); j++) { LinearLayout l_add = (LinearLayout) ((LinearLayout) l.getChildAt(j));//第二层布局layout for (int k = 0; k < l_add.getChildCount(); k++) { int viewTag_child = (int) l_add.getChildAt(k).getTag(); if(viewTag_child == ViewTypeUtil.item_view_type_five){ LinearLayout l_view = (LinearLayout) ((LinearLayout) l_add.getChildAt(k)).getChildAt(0);//第三层每一项 final EditText editText = (EditText) l_view.getChildAt(1); if (v.getTag(R.id.select_view_detial_id).equals(editText.getTag(R.id.select_view_detial_id))) { int position = (int) editText.getTag(R.id.select_view_position); mDataList.clear(); List<DicBean> dicList = new ArrayList<FormDetailBean.FormDetail.DicBean>(); if (viewTag == ViewTypeUtil.item_view_type_ten) { dicList = mFormDetailChildList.get(position).dicBeanList; }else if (viewTag == ViewTypeUtil.item_view_type_thirteen) { dicList = mFormZuDetailChildList.get(position).dicBeanList; } for (DicBean dicBean : dicList) { mDataList.add(dicBean.getDicName()); } CusSpinerAdapter<String> mAdapter = new CusSpinerAdapter<String>(this); mAdapter.refreshData(mDataList, 0); View contentView = LayoutInflater.from(context).inflate(R.layout.common_form_view, null); SpinerPopWindow mSpinerPopWindow = new SpinerPopWindow(this, R.layout.spiner_window_layout, true); mSpinerPopWindow.setAdatper(mAdapter); mSpinerPopWindow.setItemListener(new IOnItemSelectListener() { @Override public void onItemClick(int pos) { if (pos >= 0 && pos <= mDataList.size()) { String value = (String) mDataList.get(pos); editText.setText(value.toString()); } } }); mSpinerPopWindow.setWidth(editText.getWidth()); int windowPos[] = PopMeasure.calculatePopWindowPos(editText, contentView); int xOff = 200;// 可以自己调整偏移 windowPos[0] -= xOff; mSpinerPopWindow.showAtLocation(v, Gravity.TOP | Gravity.START,windowPos[0], windowPos[1]); } }else if(viewTag_child == ViewTypeUtil.item_view_type_thirteen || viewTag_child == ViewTypeUtil.item_view_type_ten){ LinearLayout l3 = (LinearLayout) ((LinearLayout) ((LinearLayout) l_add.getChildAt(k)).getChildAt(0)).getChildAt(1); for (int j3 = 0; j3 < l3.getChildCount(); j3++) { LinearLayout l_add3 = (LinearLayout) ((LinearLayout) l3.getChildAt(j3)); for (int k3 = 0; k3 < l_add3.getChildCount(); k3++) { int viewTag_child3 = (int) l_add3.getChildAt(k3).getTag(); if(viewTag_child3 == ViewTypeUtil.item_view_type_five){ LinearLayout l_view = (LinearLayout) ((LinearLayout) l_add3.getChildAt(k3)).getChildAt(0); final EditText editText = (EditText) l_view.getChildAt(1); if (v.getTag(R.id.select_view_detial_id).equals(editText.getTag(R.id.select_view_detial_id))) { int position = (int) editText.getTag(R.id.select_view_position); mDataList.clear(); List<DicBean> dicList = mFormZuDetailChildList.get(position).dicBeanList; for (DicBean dicBean : dicList) { mDataList.add(dicBean.getDicName()); } CusSpinerAdapter<String> mAdapter = new CusSpinerAdapter<String>(this); mAdapter.refreshData(mDataList, 0); View contentView = LayoutInflater.from(context).inflate(R.layout.common_form_view, null); SpinerPopWindow mSpinerPopWindow = new SpinerPopWindow(this, R.layout.spiner_window_layout, true); mSpinerPopWindow.setAdatper(mAdapter); mSpinerPopWindow.setItemListener(new IOnItemSelectListener() { @Override public void onItemClick(int pos) { if (pos >= 0 && pos <= mDataList.size()) { String value = (String) mDataList.get(pos); editText.setText(value.toString()); } } }); mSpinerPopWindow.setWidth(editText.getWidth()); int windowPos[] = PopMeasure.calculatePopWindowPos(editText, contentView); int xOff = 200;// 可以自己调整偏移 windowPos[0] -= xOff; mSpinerPopWindow.showAtLocation(v, Gravity.TOP | Gravity.START,windowPos[0], windowPos[1]); } } } } } } } } } } else if (v.getId() == R.id.et_select_part_value) { CheckPart_Monomer_Activity.actionStartForResult(context, mPartSelectValueEt.getText().toString(), "", "", monomerId, CheckPart_Monomer_Activity.class); } else if (v.getId() == R.id.cbx_type_nine) {// 类型9 多选框意向 for (int i = 0; i < mMainView.getChildCount(); i++) { int viewTag = (int) mMainView.getChildAt(i).getTag(); if (viewTag == ViewTypeUtil.item_view_type_nine) { LinearLayout l = (LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0); CheckBox checkBox = (CheckBox) l.getChildAt(1); } else if (viewTag == ViewTypeUtil.item_view_type_thirteen || viewTag == ViewTypeUtil.item_view_type_ten) { LinearLayout l = (LinearLayout) ((LinearLayout) ((LinearLayout) mMainView.getChildAt(i)).getChildAt(0)).getChildAt(1); for (int j = 0; j < l.getChildCount(); j++) { LinearLayout l_add = (LinearLayout) ((LinearLayout) l.getChildAt(j)); for (int k = 0; k < l_add.getChildCount(); k++) { int viewTag_child = (int) l_add.getChildAt(k).getTag(); if (viewTag_child == ViewTypeUtil.item_view_type_nine) { LinearLayout l_view = (LinearLayout) ((LinearLayout) l_add.getChildAt(k)).getChildAt(0); CheckBox checkBox = (CheckBox) l_view.getChildAt(1); } } } } } } else if (v.getId() == R.id.item_eleven_value_et) { for (int i = 0; i < mMainView.getChildCount(); i++) { int viewTag = (int) mMainView.getChildAt(i).getTag(); if (viewTag == ViewTypeUtil.item_view_type_eleven) { if (!TextUtils.isEmpty(mDrawingPath)) { Intent intent = new Intent(CommonFormViewActivity.this, DrawingTypeActivity.class); intent.putExtra("drawingPath", mDrawingPath); intent.putExtra("tableDetailID", (String) v.getTag()); intent.putExtra("intent_from", FLAG_UPDATE); if (mIntentFrom == FLAG_SHOW || mIntentFrom == FLAG_UPDATE) {// 查看,修改记录 intent.putExtra("RecordId", mUTableID); } startActivityForResult(intent, 11); } } } } else if (v.getId() == R.id.item_eleven_value_img_photo) {// 类型11图纸标准类型拍照按钮 mViewTypeFlag = ViewTypeUtil.item_view_type_eleven; if (mCreateBmpFactory == null) { mCreateBmpFactory = new CreateBmpFactory(CommonFormViewActivity.this); } mCreateBmpFactory.OpenCamera(); } else if (v.getId() == R.id.item_eleven_value_img_folder) {// 类型11图纸标准类型从图库选择 mViewTypeFlag = ViewTypeUtil.item_view_type_eleven; FileSelect chosen = new FileSelect(CommonFormViewActivity.this); chosen.setOnFileSelecteListener(new OnFileSelecteListener() { @Override public void onFileSelected(String filePath, String fileName) { mDrawingPath = filePath; mDrawingNameTv.setText(fileName); Intent intent1 = new Intent(CommonFormViewActivity.this, DrawingTypeActivity.class); intent1.putExtra("drawingPath", mDrawingPath); intent1.putExtra("tableDetailID", (String) mDrawingNameTv.getTag()); intent1.putExtra("intent_from", FLAG_UPDATE); if (mIntentFrom == FLAG_SHOW || mIntentFrom == FLAG_UPDATE) {// 查看,修改记录 intent1.putExtra("RecordId", mUTableID); } startActivityForResult(intent1, 11); } }); chosen.showChosenDialog(); } else if (v.getId() == R.id.item_twelve_value_path) {// 类型12 图片路径 } else if (v.getId() == R.id.item_twelve_value_img_photo) {// 类型12拍照 mViewTypeFlag = ViewTypeUtil.item_view_type_twelve; mViewTag = v.getTag(); if (mCreateBmpFactory == null) { mCreateBmpFactory = new CreateBmpFactory(CommonFormViewActivity.this); } mCreateBmpFactory.OpenCamera(); } else if (v.getId() == R.id.item_twelve_value_img_folder) {// 类型12 mViewTypeFlag = ViewTypeUtil.item_view_type_twelve; mViewTag = v.getTag(); if (mCreateBmpFactory == null) { mCreateBmpFactory = new CreateBmpFactory(CommonFormViewActivity.this); } mCreateBmpFactory.OpenGallery(); } else if (v.getId() == R.id.iv_type_ten_add) { AddListChildView(); } }
估计这么写看看都会头晕,因为这上面代码 里面除了处理正常的点击事件,还要处理列表和组 也就是 嵌套布局里面的点击事件。但是原理都是一样的。
就是循环布局找到 你点击的那个布局,那个控件... 如果是嵌套多级 就一级一级的找...
5.保存上传数据
我们这边保存处理的逻辑就是将界面上所有的数据都拼成一个JSONOject 里面套JSONArray和JSONObject ,反正就是根据自己的业务往里面套数据,这个数据组织起来稍微有一点点麻烦。
代码贴一下:
package com.jky.xmxtcommonlib.utils; import org.json.JSONException; import org.json.JSONObject; import com.jky.xmxtcommonlib.R; import android.widget.CheckBox; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; /** * Created by zlw on 2017/6/13. */ public class GetViewValueUtil { // 表格输入框取值方法 public static JSONObject getTypeZeroValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { EditText editText = (EditText) linearLayout.getChildAt(1); String id = editText.getTag() + ""; String str = editText.getText().toString().trim(); jsonObject.put(id + "", str); return jsonObject; } // 单行输入框取值方法 public static JSONObject getTypeOneValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { EditText editText = (EditText) linearLayout.getChildAt(1); String id = editText.getTag() + ""; String str = editText.getText().toString().trim(); jsonObject.put(id + "", str); return jsonObject; } // 多行输入框取值方法 public static JSONObject getTypeTwoValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { EditText editText = (EditText) linearLayout.getChildAt(1); String id = editText.getTag() + ""; String str = editText.getText().toString().trim(); jsonObject.put(id + "", str); return jsonObject; } // 时间选择框取值方法 public static JSONObject getTypeThreeValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { if(linearLayout !=null){ EditText editText = (EditText) linearLayout.getChildAt(1); if(editText!=null){ String id = editText.getTag() + ""; String str = editText.getText().toString().trim(); jsonObject.put(id + "", str); }else{ JSONObject json= new JSONObject(); return json; } } else { JSONObject json= new JSONObject(); return json; } return jsonObject; } // 数值输入框取值方法 public static JSONObject getTypeFourValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { EditText editText = (EditText) linearLayout.getChildAt(1); String id = editText.getTag() + ""; String str = editText.getText().toString().trim(); jsonObject.put(id + "", str); return jsonObject; } // 下拉选择框取值方法 public static JSONObject getTypeFiveValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { EditText editText = (EditText) linearLayout.getChildAt(1); String id = editText.getTag(R.id.select_view_detial_id) + ""; String str = editText.getText().toString().trim(); jsonObject.put(id + "", str); return jsonObject; } // 部位选择框取值方法 public static JSONObject getTypeSevenValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { EditText editText = (EditText) linearLayout.getChildAt(1); String id = editText.getTag() + ""; String str = editText.getText().toString().trim(); jsonObject.put(id + "", str); return jsonObject; } // 单选框取值方法 public static JSONObject getTypeEightValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { RadioGroup radioGroup = (RadioGroup) linearLayout.getChildAt(1); for (int i = 0; i < radioGroup.getChildCount(); i++) { RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i); String str = (String) radioButton.getTag(); String detailId = str.split(",")[0]; String dicId = str.split(",")[1]; if (radioButton.isChecked()) { jsonObject.put(detailId + "", dicId); } } return jsonObject; } // 多选框取值方法 public static JSONObject getTypeNineValue(LinearLayout linearLayout, JSONObject jsonObject) throws JSONException { CheckBox checkBox = (CheckBox) linearLayout.getChildAt(1); String value = ""; String str = (String) checkBox.getTag(); if (checkBox.isChecked()) { value = checkBox.getText().toString().trim(); String detailId = str.split(",")[0]; String dicId = str.split(",")[1]; jsonObject.put(detailId,dicId); } else { String detailId = str.split(",")[0]; jsonObject.put(detailId, "0"); } return jsonObject; } // 图纸类表格类型存值 public static JSONObject getTypeElevenValue(LinearLayout linearLayout,String path, JSONObject jsonObject) throws JSONException { TextView extView = (TextView) linearLayout.getChildAt(1); String id = extView.getTag() + ""; String str = path.trim(); jsonObject.put(id + "", str); return jsonObject; } // 照片类表格类型存值 public static JSONObject getTypeTwelveValue(LinearLayout linearLayout,String path, JSONObject jsonObject) throws JSONException { TextView extView = (TextView) linearLayout.getChildAt(0); String id = extView.getTag() + ""; String str = path.trim(); jsonObject.put(id + "", str); return jsonObject; } }
/** * 得到需要保存的json数据 * * @param linearLayout * @param isChildFlag * @param jsonKey * @throws JSONException */ private void getJsonInfo(LinearLayout linearLayout) throws JSONException { for (int i = 0; i < linearLayout.getChildCount(); i++) { int viewTag = (int) linearLayout.getChildAt(i).getTag(); if (viewTag == ViewTypeUtil.item_view_type_zero) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeZeroValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_one) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeOneValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_two) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeTwoValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_three) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeThreeValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_four) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeFourValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_five) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeFiveValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_six) { } else if (viewTag == ViewTypeUtil.item_view_type_seven) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeSevenValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_eight) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeEightValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_nine) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeNineValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_ten) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); LinearLayout layout = (LinearLayout) l.getChildAt(1); getSecondJsonInfo(layout, (String) layout.getTag()); mJsonObject.put((String) layout.getTag(), mSecondJsonArray); } else if (viewTag == ViewTypeUtil.item_view_type_eleven) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); if (!TextUtils.isEmpty(mDrawingPath)) { mJsonObject = GetViewValueUtil.getTypeElevenValue(l, mDrawingPath, mJsonObject); } } else if (viewTag == ViewTypeUtil.item_view_type_twelve) { if(!TextUtils.isEmpty(mPhotoPath)){ LinearLayout l = (LinearLayout) ((LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0)).getChildAt(1); mJsonObject = GetViewValueUtil.getTypeTwelveValue(l, mPhotoPath, mJsonObject); } } else if (viewTag == ViewTypeUtil.item_view_type_thirteen) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); LinearLayout layout = (LinearLayout) l.getChildAt(1); getSecondJsonInfo(layout, (String) layout.getTag()); mJsonObject.put((String) layout.getTag(), mSecondJsonArray); } else if (viewTag == ViewTypeUtil.item_view_type_fourteen) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeThreeValue(l, mJsonObject); } else if (viewTag == ViewTypeUtil.item_view_type_fifteen) { LinearLayout l = (LinearLayout) ((LinearLayout) linearLayout.getChildAt(i)).getChildAt(0); mJsonObject = GetViewValueUtil.getTypeThreeValue(l, mJsonObject); } } }