动态加载布局和控件的方式实现动态加载(用listView解决EditText问题挺麻烦)

//第一步要声明些map来保存这些控件

Map<String, EditText> map_edit ;  // 根据 mxb中的 autobh ->memo封装  控件
 Map<String, String> map_v ;// 封装mxb中的autobh->jcjg
 List<String> list_bh ; // 封装mxb中  autobh
 Map<String, String> map_jcnr = new HashMap<String, String>(); // 封装mxb中的autobh->jcnr:为发现问题设置

 

 

//第二步

刚进来的时候,先需要获取一些基本数据,比如这里获得一个对象数组

 cyjxdList=czff.GetList_Person(MainActivity.this,zfrybh, mima, bsh);

 

第三步,根据获取到的数据,要填入相应的内容了,并且获取索输入的内容,addview在handleMessage(Message msg)中可以调用,


/*生成视图1*/
 @SuppressLint("NewApi")
 private void addview() {
  // TODO Auto-generated method stub
  list_bh = new ArrayList<String>();
  for(int i=0;i<size;i++)
  {
   //new 一个线性布局   准备装填内容
   LinearLayout linear = new LinearLayout(this);
   linear.setBackgroundColor(color[i%2]);
   linear.setOrientation(LinearLayout.VERTICAL);
  
   //获取当前的一个对象,保存下
         final Person info = Persons.get(i);
   TextView text = new TextView(this);
   //比如这里书写调查的内容
   text.setText((++idex)+"、"+info.JCNR);
   text.setTextSize(text_size_xmmc);
   text.setTextColor(getResources().getColor(R.color.black));
   LayoutParams l = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   //设置距离左上右下位置
   l.setMargins(10, 15, 0, 0);
   linear.addView(text,l);
   //把perosn的num放在list_bh的list中,把它记录下来
   list_bh.add(info.personNum+"");

   //下面再新建一个先对布局了
   RelativeLayout linear1 = new RelativeLayout(this);
   RelativeLayout.LayoutParams lll = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
   lll.setMargins(25, 15, 0, 0);

     //合格,不合格   放在一个线性布局中
    LinearLayout lin_1 = new LinearLayout(this);
    lin_1.setOrientation(LinearLayout.HORIZONTAL);

    final MyCheck r1 = new MyCheck(this, null);
    r1.setTextSize(text_size_check);
    r1.setTextColor(getResources().getColor(R.color.black));
    r1.setText("合格");

    final MyCheck r2 = new MyCheck(this, null);
    r2.setTextSize(text_size_check);
    r2.setTextColor(getResources().getColor(R.color.black));
    r2.setText("不合格");

    lin_1.addView(r1);
    lin_1.addView(r2);
    linear1.addView(lin_1,lll);
                //记录当前的检查结果合格不合格
    map_v.put(info.AUTOBH+"","空");

    r1.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View arg0) {
      // TODO Auto-generated method stub
      info.JCJG ="男" ;
      r1.setChecked(true);
      r2.setChecked(false);
      //如果合格,map中的值该成合格。
      map_v.put(info.AUTOBH+"","男");
     }
    });
    r2.setOnClickListener(new OnClickListener() {

     @Override
     public void onClick(View arg0) {
      // TODO Auto-generated method stub
      info.JCJG ="女" ;
      r1.setChecked(false);
      r2.setChecked(true);
      map_v.put(info.AUTOBH+"","女");
     }
    });

  
   //这里要搞一个备注了
   EditText ed = new EditText(this);
   ed.setTextSize(text_size_check);
   ed.setTextColor(getResources().getColor(R.color.black));
   ed.setSingleLine(true);

   DisplayMetrics dm = new DisplayMetrics();
   getWindowManager().getDefaultDisplay().getMetrics(dm);
   int screenW = dm.widthPixels;

   RelativeLayout.LayoutParams llll = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
   llll.setMargins(screenW/2, 15, 25, 0);
             //map中保存当前的备注Edittext
   map_edit.put(info.personNum+"",ed);
   //检查内容也把它放在检查内容中
   map_jcnr.put(info.personNum+"",info.JCJG);

   linear1.addView(ed,llll);
           //edittext回调实现,这里可以把它显示出来
   ed.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
     // TODO Auto-generated method stub
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
      int after) {
     // TODO Auto-generated method stub
    }
    @Override
    public void afterTextChanged(Editable s) {
     // TODO Auto-generated method stub
    }
   });

   linear.addView(linear1);
   zdjc_cyjxd_lin.addView(linear);
  }
  //当前的第一个布局得edittext获取焦点
  map_edit.get(list_bh.get(0)).requestFocus();
 }

转载于:https://my.oschina.net/u/2480757/blog/600102

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值