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



在我的应用中,用户输入一个数字(在这种情况下,化学方程式中的反应物数量,因此起始材料的数量),结果有许多框,按下按钮后,在下面产生/产生/创建的用户给出的数字的数量匹配。

我该怎么做?我以为会发挥作用,所以我开始了。

这是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>
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);
}

,如果您想从这些诚意中获取数据,请在代码中的其他位置保留一个deDittext的数组,然后将这些EditText对象保存在该数组中..,。

尝试这个..,。

最新更新