从微调器动态设置Edittext

时间:2014-03-19 10:46:26

标签: android android-edittext android-spinner

我有一个微调器,它有1,2,3项等等。在微调器中选择特定项时,edittexts应根据微调器的数量动态显示。

如果我从下拉菜单中选择3,我将动态获得三个edittexts,但如果我选择2则不再显示2个edittexts ...请帮助我解决。

我附上了我的代码..            包com.example.spinner;

import static android.view.ViewGroup.LayoutParams.FILL_PARENT;
  import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
    import static android.widget.LinearLayout.VERTICAL;

   import java.util.ArrayList;
  import java.util.List;

  import android.R.integer;
  import android.app.Activity;
  import android.graphics.Color;
  import android.os.Bundle;
  import android.view.Menu;
   import android.view.View;
   import android.view.ViewGroup;
  import android.view.ViewGroup.LayoutParams;
   import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemSelectedListener;
   import android.widget.ArrayAdapter;
import android.widget.Button;
 import android.widget.EditText;
  import android.widget.LinearLayout;

import android.widget.RelativeLayout;         import android.widget.Spinner;

  public class spin_activity extends Activity {
private Spinner sp;
EditText et1, et2;
List<String> list;
RelativeLayout rl;
boolean isspinnerselected = false;
private List<EditText> editTextList = new ArrayList<EditText>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner);
    rl = (RelativeLayout) findViewById(R.id.r1);
    System.out.println("####################################");
    setitem();
}

private void setitem() {

    /*
     * sp =(Spinner)findViewById(R.id.spinner1);
     * 
     * 
     * 
     * ArrayAdapter<CharSequence> data =
     * ArrayAdapter.createFromResource(getApplicationContext(),
     * R.array.Fruits, android.R.layout.simple_spinner_item);
     * data.setDropDownViewResource
     * (android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(data);
     * sp.setOnItemSelectedListener(this);
     */
    sp = (Spinner) findViewById(R.id.spinner1);
    list = new ArrayList<String>();
    list.add("How many kids?");
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");

    ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, list);
    adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

    sp.setAdapter(adp);
    System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
    sp.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
                long id) {

            System.out.println("positoin" + pos);
            if (isspinnerselected) {
                final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        (int) LayoutParams.WRAP_CONTENT,
                        (int) LayoutParams.WRAP_CONTENT);
                // et1.setFocusable(true);
                params.leftMargin = 80;
                params.topMargin = 100;
                System.out.println("Spinner selected");

                int val = Integer.parseInt(sp.getSelectedItem().toString());
                System.out.println("value of val" + val);
                System.out.println("Child count: " + rl.getChildCount());
                if (rl.getChildCount() >1 ) {
                    System.out.println("*************");
                    rl.removeViews(1, rl.getChildCount()-2);
                    System.out.println("Aft removng child, count is: "
                            + rl.getChildCount());
                }
                for (int i = 0; i < val; i++)

                {

                    System.out.println("value of i: " + i);
                    System.out.println("BEFORE>>>>>>Params top margin: "
                            + params.topMargin);
                    et1 = new EditText(spin_activity.this);

                    et1.setFocusable(true);
                    et1.setLayoutParams(params);

                    et1.setHint("EditText" + params.topMargin);


                    et1.setBackgroundColor(Color.GREEN);
                    // et1.setLayoutParams(new
                    // RelativeLayout.LayoutParams(source))
                    rl.addView(et1);
                    System.out.println("Child count aft adding et: "
                            + rl.getChildCount());
                    params.topMargin += 100;
                    System.out.println("AFTER>>>>>>Params top margin: "
                            + params.topMargin);
                }

            } else
                System.out.println("Spinner selected is false");
            isspinnerselected = true;
            /*
             * et2 = new EditText (spin_activity.this);
             * RelativeLayout.LayoutParams _params = new
             * RelativeLayout.LayoutParams ((int) LayoutParams.WRAP_CONTENT,
             * (int) LayoutParams.WRAP_CONTENT); et2.setFocusable(true);
             * _params.leftMargin = 80; _params.topMargin = 280;
             * et2.setLayoutParams(_params); et2.setHint("EditText");
             * et2.setEms(10); rl.addView(et2);
             */

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
}

/*
 * @Override public void onItemSelected(AdapterView<?> parent, View arg1,
 * int pos, long arg3) { System.out.println("positoin"+pos);
 * 
 * if(pos==0) { EditText et1 = new EditText (this);
 * RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
 * ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
 * 
 * params.leftMargin = 80; params.topMargin = 180;
 * et1.setLayoutParams(params); et1.setHint("EditText"); et1.setEms(10); }
 * 
 * if(pos==1) { editText("name"); }
 * 
 * 
 * switch (pos) { case 0: EditText et1 = new EditText (this);
 * 
 * RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams
 * ((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
 * 
 * params.leftMargin = 80; params.topMargin = 180;
 * 
 * et1.setLayoutParams(params);
 * 
 * et1.setHint("EditText"); et1.setEms(10);
 * 
 * 
 * break; case 1: // What ever you want to happen when item selected break;
 * case 2: // What ever you want to happen when item selected break;
 * 
 * }
 * 
 * } private EditText editText(String hint) { EditText editText = new
 * EditText(this); editText.setId(Integer.valueOf(hint));
 * editText.setHint(hint); editTextList.add(editText); return editText; }
 * 
 * @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO
 * Auto-generated method stub
 * 
 * }
 */
    }

0 个答案:

没有答案