将EditText视图动态添加到水平滚动视图

时间:2012-10-31 16:54:27

标签: android for-loop android-edittext horizontalscrollview

在我的应用程序中,用户输入一个数字(在这种情况下,化学方程中的反应物数量,因此起始材料的数量),结果是一些数字框,数量与给定的数量相匹配按下按钮后,由用户生成/生成/创建。

我该怎么做呢?我以为for-loop会起作用,所以我把它开始了。

这是Java类:

public class EquationBalancer extends Activity {

    final EditText reactantNumberField = (EditText) findViewById(R.id.reactantsNumber);
    final int reactantNom = Integer.parseInt(reactantNumberField.getText().toString());

    HorizontalScrollView reactants = (HorizontalScrollView) findViewById(R.id.reactants);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.balancelayout);

        Button setReactantsNo = (Button) findViewById(R.id.reactantsNumberOk);
        setReactantsNo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for (int i = 1; i < reactantNom; i++) {
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.balancelayout, menu);
        return true;
    }

}

和XML布局:

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

    <TextView
        android:id="@+id/reactantsHowMany"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:text="@string/reactantsHowMany"
        android:textSize="18dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/reactantsNumber"
        android:paddingTop="5dp"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/reactantsHowMany"
        android:layout_marginLeft="60dp"
        android:inputType="number"
        android:hint="e.g. 4" />

    <Button 
        android:id="@+id/reactantsNumberOk"
        android:paddingTop="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/reactantsHowMany"
        android:layout_alignParentRight="true"
        android:layout_marginRight="60dp"
        android:layout_alignBottom="@id/reactantsNumber"
        android:text="Set" />

    <HorizontalScrollView
        android:id="@+id/reactants"
        android:paddingTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@id/reactantsNumber" >

    </HorizontalScrollView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

for (int i = 1; i < reactantNom; i++) {
    EditText editText=new EditText(this);
    editText.setText("some text if you want else remove this line");
    reactants.addView(editText);
}

如果你想从这些edittext中获取数据,那么代码中的其他地方就会维护一个edittext数组并将这些edittext对象保存在该数组中..,。

试试这个..,。

相关问题