动态加载控件并在外部得到控件

在xml中写好的布局是这样的:


布局文件(只截取了一分部):

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_settings_mid"
    android:gravity="center"
    android:id="@+id/new_add_layout"
    android:orientation="vertical">

    <TextView
        android:id="@+id/apply_use_type_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@null"
        android:layout_gravity="center_vertical"
        android:paddingLeft="8dp"
        android:text="选择用车类型和数量:"
        android:textColor="@android:color/black"
        android:textSize="@dimen/tv_size_apply_car"
        android:src="@drawable/car_easy_usedays" />

</LinearLayout>


然后在Activity中请求后台,得到后台的数据中,才开始加载控件。达到下面的效果:


/**
 * 动态加载车辆
 */
public void dynamicView() {
    List<Integer> carNumList;
    List<String> carTypeList;
    List<Integer> setCarNum;
    LinearLayout lin;
    LinearLayout new_add_layout = null;

    carNumList = lYGUserInfo.getCarnum();
    carTypeList = lYGUserInfo.getCartype();
    setCarNum = new ArrayList<>();

    int carSize = carTypeList.size();
    lin = new LinearLayout(this);
    lin.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams LP_FW = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout newSingleRL;

    for (int i = 0; i < carSize; i++) {
        String checkname = carTypeList.get(i) + "(" + carNumList.get(i) + ")";
        newSingleRL = generateSingleLayout(editTextId + i, checkBoxId + i, checkname, carNumList.get(i));
        lin.addView(newSingleRL, LP_FW);
    }
    new_add_layout.addView(lin);
}

carNumList和carTypeList是从后台得到的。

  "carnum": [
    5,
    16,
    2,
    3
  ],
  "cartype": [
    "SUV",
    "轿车",
    "客车",
    "商务车"
  ],

在最外层的LinearLayout中放一个LinearLayout 然后for循环放RealtiveLayout 左边checkbox  右边edittext。

/**
 * 添加 checkbox+edittext的方法
 *
 *  @param editId
 * @param chechkId
 * @param str      checkbox的名字
 * @param carNum   当前车辆数
 * @return
 */
private RelativeLayout generateSingleLayout(final int editId, int chechkId, String str, final int carNum) {
    layout_root_relative = new RelativeLayout(this);

    final CheckBox cb = new CheckBox(this);
    LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    cb.setText(str);
    cb.setId(chechkId);
    cb.setTextSize(20);
    cb.setLayoutParams(LP_WW);
    layout_root_relative.addView(cb);

    final EditText et = new EditText(this);
    RelativeLayout.LayoutParams RL_WW = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    RL_WW.setMargins(100, 5, 10, 5);
    et.setWidth(100);
    et.setPadding(5, 5, 5, 5);
    RL_WW.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    et.setLayoutParams(RL_WW);
    et.setId(editId);
    et.setInputType(InputType.TYPE_CLASS_NUMBER);
    et.setCursorVisible(false);      //设置输入框中的光标不可见
    et.setFocusable(false);          //无焦点
    et.setFocusableInTouchMode(false);
    layout_root_relative.addView(et);

    //监听edit
    et.addTextChangedListener(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){
            KLog.e(et.getId());
            int editStr = Integer.parseInt(et.getText().toString());
            if (editStr > carNum){
                carNumBoolean = false;
            }else{
                carNumBoolean = true;
            }
        }
    });

    cb.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            if (cb.isChecked()) {
                Toast.makeText(getApplication(), cb.getId() + "勾选,edit:" + et.getId(), Toast.LENGTH_SHORT).show();
                et.setCursorVisible(true);
                et.setFocusable(true);
                et.setFocusableInTouchMode(true);
            } else {
                Toast.makeText(getApplication(), cb.getId() + "未勾选", Toast.LENGTH_SHORT).show();
                et.setCursorVisible(false);
                et.setFocusable(false);
                et.setFocusableInTouchMode(false);
            }
        }
    });
    return layout_root_relative;
}


后面需要得到edittext中输入的数量,用一个list来放:

for (int i = 0; i < lin.getChildCount(); i++) {
    RelativeLayout relativeLayout = (RelativeLayout) lin.getChildAt(i);
    EditText editText = (EditText) relativeLayout.getChildAt(0);
    setCarNum.add(Integer.valueOf(editText.getText().toString()));
    KLog.e(setCarNum);
}

将车辆类型和数量封装为json传给后台:

//apply_yongche_num:[{"客车":"2"},{"SUV":"3"},{"商务车":"4"},{"轿车":"15"}]
JSONArray carTypeJson = new JSONArray();
for(int i=0;i<carTypeList.size();i++){
    JSONObject carTypeObj=new JSONObject();
    String carType=carTypeList.get(i);
    int carnum=setCarNum.get(i);
    try {
        carTypeObj.put(carType,carnum);
        carTypeJson.put(carTypeObj);
    } catch (JSONException ex){
        throw new RuntimeException(ex);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值