问题:
前端自己拖拽控件的界面,后台将界面中的控件字段传来,有些是EditText,有些是时间控件,有些是TextView,而且顺序不一样,所以手机端这边如何形成这个界面呢
解决方法:
步骤一:与后台指定空间相对应的空间名称,动态添加相对应控件id
private static int singleId =100;
private static int multipleId =101;
private static int currencyId =102;
步骤二:后台传来数据后,根据字段判断是要创建哪个控件界面,这个部分觉得代码还可以优化,但是目前还没想好怎么改,就先这样吧
for (CustomDetailData item :result) {
if (item.getType().equals("SingleEditText")) {//单行
LinearLayoutnewLiEd = generateSingleEdit(singleId, item.getName());
linear.addView(newLiEd, LP_FW);
}else if (item.getType().equals("MultipleEditText")) {//多行
LinearLayoutnewLiEd = generateMultipleEdit(multipleId, item.getName());
linear.addView(newLiEd, LP_FW);
}else if (item.getType().equals("CurrencyEditText")) {//货币
LinearLayoutnewLiEd = generateCurrencyEdit(currencyId, item.getName());
linear.addView(newLiEd, LP_FW);
}
步骤三:跳转到相对应控件生成的地方(实例为多行输入的关键部分)
private String editTextMStr ="";
public LinearLayoutgenerateMultipleEdit(int singleId, String name) {
LinearLayoutlayout_root_relative =new LinearLayout(this);
layout_root_relative.setBackgroundResource(R.color.white);
layout_root_relative.setOrientation(LinearLayout.VERTICAL);
LinearLayoutlayout_sub_Lin =new LinearLayout(this);
layout_sub_Lin.setBackgroundColor(Color.argb(255, 255, 255, 255));
layout_sub_Lin.setOrientation(LinearLayout.HORIZONTAL);
layout_sub_Lin.setPadding(10, 10, 10, 10);
TextViewtv =new TextView(this);
LinearLayout.LayoutParamsLP_WW =new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setText(name);
tv.setPadding(10, 10, 0, 0);
tv.setTextColor(Color.argb(0xff, 0x00, 0x00, 0x00));
tv.setTextSize(16);
tv.setLayoutParams(LP_WW);
layout_sub_Lin.addView(tv);
LinearLayout.LayoutParamsRL_MW =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);//尤其注意这个位置,用的是父容器的布局参数
RL_MW.setMargins(0, 2, 0, 2);
layout_root_relative.addView(layout_sub_Lin, RL_MW);
EditTexteditText =new EditText(this);
LinearLayout.LayoutParamsRL_ED =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
RL_ED.setMargins(10, 8, 10, 8);
editText.setPadding(10, 8, 10, 8);//多行输入参数在这里设置
editText.setBackgroundResource(R.drawable.pop_windows_background);
editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setTextSize(16);
editText.setGravity(Gravity.TOP);
editText.setHorizontallyScrolling(false);
editText.setLines(6);
editText.setLayoutParams(RL_ED);
// final FormDataformData =new FormData();
// editText.setText(formData.getValue());
TextWatcherwatcher =new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (TextUtils.isEmpty(s)) {
//formData.setValue("未填");
}else {
//formData.setValue(s.toString());
}
}
};
editText.addTextChangedListener(watcher);
editText.setTag(watcher);
// formData.setLabel(name);
//formData.setValue(editTextMStr);
//formListData.dataList.add(formData);
editText.setId(singleId);//设置控件ID
layout_root_relative.addView(editText);
return layout_root_relative;
}
额外需注意不是EdiText控件的时候得注意一下因为EditText用TextWatch监听不会出现数据重复的问题,当控件为Spinner或者CheckBox等控件时选择好选项后再去选择可能数据会重复,为防止数据重复,最好做下判断是否数据重复了。