在Android中使用edittext和spinner子项动态创建复选框

时间:2014-01-18 01:45:45

标签: java android android-layout dynamically-generated

我要做的是选择任何项目,从微调器,动态创建一个复选框,当复选框被选中后,我用一个微调器动态创建edittext。我得到了布局,但我不知道如何处理edittext和选定的微调项目(可见)并将其附加到字符串。

  

例如:checkeditem:childspinner; checkitem:childspinner等

以下是我尝试过但我无法获取文本,旋转项目并将其放入格式中。任何帮助将不胜感激。

checkBoxes = new CheckBox(Activity1.this);
checkBoxes.setText(materials[i]);

String matId = materialListIdMap.get(materials[i]);
checkBoxes.setId(Integer.parseInt(matId));
materialsUsedLayout.addView(checkBoxes);

EditText edittext=new EditText(Activity1.this);
edittext.setHint("Count");
allEds.add(edittext);
edittext.setVisibility(View.GONE);

Spinner uomSpinner = new Spinner(Activity1.this);
uomSpinner.setAdapter(unitofMeasurementAdapter);
uomSpinner.setVisibility(View.GONE);

materialsUsedLayout.addView(edittext);
materialsUsedLayout.addView(uomSpinner);


checkBoxes.setOnClickListener(getOnClickDoSomething(checkBoxes, edittext, uomSpinner));

View.OnClickListener getOnClickDoSomething(final CheckBox button, final EditText      edittext, final Spinner uomSpinner)
{
    return new View.OnClickListener() 
    { 
        public void onClick(View v) { 
            if (button.isChecked()) {

                edittext.setVisibility(View.VISIBLE);
                uomSpinner.setVisibility(View.VISIBLE);

                uomSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
                    {           
                        Object obj = parent.getItemAtPosition(pos);   
                        String newtext=edittext.getText().toString();
                        uomid=unitofMeasurementIdMap.get(obj);

                        System.out.println("*************id**"+newtext+"****" + button.getId()+"\\   \\"+uomid); 

                        StringBuffer edtBuffer = new StringBuffer();
                        edtBuffer.append(button.getId()+":"+uomid+";");
                    }
                    public void onNothingSelected(AdapterView<?> parent) { }    

                });

                System.out.println("*************id******" + button.getId()+"\\   \\"+uomid); 
                System.out.println("and text***" + button.getText().toString()); 

                //System.out.println("buffer: "+edtBuffer);
            }
            else
            {
                edittext.setVisibility(View.GONE);
                uomSpinner.setVisibility(View.GONE);
                System.out.println("no checking");
            }
        } 
    };

0 个答案:

没有答案