动态添加EditText时没有任何事情发生

时间:2014-01-15 12:32:51

标签: android android-edittext

在我的Android项目中,我试图从Spinner的值动态添加EditText但没有发生任何事情。假设在Spinner中我选择了两个然后它应该在Layout中添加2个EditText或者选择3然后它应该在Layout中添加3个EditText。

我已经尝试并编写了这段代码,

Spinner spinner;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    spinner=(Spinner)findViewById(R.id.spinner);
    ArrayList<Integer> options=new ArrayList<Integer>();
    for(int i=1;i<7;i++)
    {
        options.add(i);
    }
    ArrayAdapter<Integer> adapter=new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item,options);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) 
        {
            //Integer.parseInt(spinner.getSelectedItem().toString());
            //int a=spinner.getSelectedItemPosition();
            addEditView();
        }

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

        }

    });
}

public void addEditView()
{
    final LinearLayout ll=new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    EditText et=new EditText(this);
    et.setText("Edit text");
    ll.addView(et);

}

XML文件,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/noofedittexttv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="30dp"
    android:text="No of EditText "
    android:layout_marginRight="5dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="30dp"
    android:layout_toRightOf="@+id/noofpassengerstv"
    android:background="@drawable/rounded_edittext" />

</RelativeLayout>

请建议我该怎么办?

感谢。

3 个答案:

答案 0 :(得分:0)

你走了。如上所述,您需要将动态创建的TextView添加到容器中,在本例中为LinearLayout:

main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/main_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="50dp"
         >

        <TextView
            android:id="@+id/noofedittexttv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="30dp"
            android:text="No of EditText "
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"

        />

</RelativeLayout>

MainActivity.java:

package com.example.test;

import java.util.ArrayList;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.text.Layout;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class MainActivity extends Activity {

    Spinner spinner;
    private LinearLayout textViewContainer;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinner = (Spinner) findViewById(R.id.spinner);
        textViewContainer = (LinearLayout) findViewById(R.id.main_container);

        ArrayList<Integer> options = new ArrayList<Integer>();
        for (int i = 1; i < 7; i++) {
            options.add(i);
        }
        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,
                android.R.layout.simple_spinner_item, options);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

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

            }

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

                addEditView(spinner.getSelectedItem());

            }

        });
    }

    public void addEditView(Object o) {
        textViewContainer.removeAllViews();
        Integer n = (Integer) o;

        for(int i = 0; i < n; i++) {
            EditText et = new EditText(this);
            et.setText("Edit text: " + i);
            textViewContainer.addView(et);
        }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

答案 1 :(得分:0)

你的问题是你正在为一个新的LinearLayout添加一个视图并且没有添加LinearLayout,如果你想在你的布局main中添加一个EditText你应该从你的XML文件中获取你的LinearLayout,如下所示: p>

public void addEditView()
{
    final LinearLayout ll= (LinearLayout) findViewById(R.id.my_layout); //this id has been defined in your layout xml using parameter "android:id"
    EditText et=new EditText(this);
    et.setText("Edit text");
    ll.addView(et);

}

希望有所帮助

答案 2 :(得分:0)

在xml文件中,编写以下代码,以添加edittexts:

<LinearLayout
    android:id="@+id/linearlayout_edittext"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

并替换您的代码,如:

LinearLayout linearLayoutEdittext;
Spinner spinner;
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    spinner=(Spinner)findViewById(R.id.spinner);
    linearLayoutEdittext=(LinearLayout)findViewById(R.id.linearlayout_edittext);
    ArrayList<Integer> options=new ArrayList<Integer>();
    for(int i=1;i<7;i++)
    {
        options.add(i);
    }
    ArrayAdapter<Integer> adapter=new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item,options);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() 
    {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) 
        {
            //Integer.parseInt(spinner.getSelectedItem().toString());
            //int a=spinner.getSelectedItemPosition();
            addEditView();
        }

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

        }

    });
}
/******************* EDITED ******************/
    public void addEditView(){
    int n = Integer.parseInt(spinner.getSelectedItem().toString());
    for(int i = 0; i<n; i++){
        EditText et=new EditText(this);
        et.setText("Edit text");
        linearLayoutEdittext.addView(et);
    }
}