在动态布局android中保存edittext的值

时间:2013-11-11 15:29:01

标签: android

我创建了动态布局,从xml中选取数据,然后根据它创建布局。

我想保留我在edittext中写的内容,从而获得填充值。 但我没有成功。

public void generateFields()
{

    ll.removeAllViewsInLayout();
    for(;i<=GenericXMLParser.nooffields;)
    {

        for(int j=i;j<i+3  ;j++)
        {


            if((j+1)>GenericXMLParser.nooffields)
            {
                break;
            }
            if(isonphone.get(j).equalsIgnoreCase("false"))
            {
                Log.i("isonphone", isonphone.get(j));

                TextView textview = new TextView(this);
                textview.setText(field.get(j));
                textview.setTextSize(30);

                textview.setTextColor(Color.parseColor("#000000"));
                ll.addView(textview);

                if(typefield.get(j).equalsIgnoreCase("text"))
                {
                    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    llp.setMargins(0, 5, 0, 15); // llp.setMargins(left, top, right, bottom);

                    EditText et = new EditText(this);
                    et.setHint(field.get(j));
                    et.setTextSize(30);
                    et.setLayoutParams(llp);
                    et.setTextColor(Color.parseColor("#000000"));
                    et.setBackgroundResource(R.drawable.genericedittext);
                    ll.addView(et);

                    savePreferences("field"+j,et.getText().toString().trim());

                    texts.add(et.getText().toString().trim());
                    Toast.makeText(getApplicationContext(), "field"+i+texts, 1).show();


                }
                else if(typefield.get(j).equalsIgnoreCase("boolean"))
                {
                    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    llp.setMargins(0, 5, 0, 15); // llp.setMargins(left, top, right, bottom);

                    RadioGroup rg = new RadioGroup(this);
                    RadioButton rb1 = new RadioButton(this);
                    RadioButton rb2 = new RadioButton(this);
                    rg.setOrientation(RadioGroup.HORIZONTAL);
                    rg.setLayoutParams(llp);
                    rb1.setText("Yes");
                    rb2.setText("No");

                    rb1.setTextColor(Color.parseColor("#000000"));
                    rb2.setTextColor(Color.parseColor("#000000"));
                    rb1.setTextSize(30);
                    rb2.setTextSize(30);
                    rg.addView(rb1);
                    rg.addView(rb2);

                    ll.addView(rg);

                }
                else if(typefield.get(j).equalsIgnoreCase("numeric"))
                {
                    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                    llp.setMargins(0, 5, 0, 15); // llp.setMargins(left, top, right, bottom);

                    EditText et = new EditText(this);
                    et.setHint(field.get(j));
                    et.setTextSize(30);
                    et.setLayoutParams(llp);
                    et.setInputType(InputType.TYPE_CLASS_NUMBER);
                    et.setTextColor(Color.parseColor("#000000"));
                    et.setBackgroundResource(R.drawable.genericedittext);
                    ll.addView(et);
                }

            }

            this.setContentView(rl);


        }
        break;


    }
    //Toast.makeText(getApplicationContext(), "i="+i, 1).show();


    if(i>GenericXMLParser.nooffields)
    {
        //Take me to the summary page.
        Intent gmsIntent = new Intent(getApplicationContext(),
                //GateManagementActivity.class);
                GateManagementActivity.class);
        gmsIntent.putExtra("userId", userId);
        gmsIntent.putExtra("clientId", clientId);
        startActivity(gmsIntent);
    }
    i+=3;
}

0 个答案:

没有答案