2012-10-31 148 views
1

在我的应用程序中,用户输入一个数字(在这种情况下,化学方程中的反应物数量,所以起始材料的数量)以及作为结果的一些框,与用户给出的数字相匹配的数量在按下按钮之后被产生/产生/创建。动态添加EditText视图到水平滚动视图

我该如何去做这件事?我认为for循环会起作用,所以我开始了。

这里的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> 

回答

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对象保存在该数组中..,。

试试这个..,。

+0

我很确定这正是我所寻找的。谢谢。现在只有一个关闭力量的问题。 – MiKenning

+0

强行关闭,但为什么..,。 – user1690588

+0

不是因为这个。在我试图解决这个问题之前有这个问题。我看了看,然后在清单中错误地宣布了这个活动,并纠正了它,但它仍然无法正常工作。 – MiKenning