I want to开发者_开发问答 create a Editbox dynamically when i click a add button and i also want to get the values typed in that Editbox when i click in save button.
Please Help me. Regards. Augustine
Try out this:
LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout = (LinearLayout)findViewById(R.id.mylinearlayout);
Button lButton = (Button)findViewById(R.id.mybtnid);
lButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText lEditText = new EditText(this);
lEditText.SetText("Text Here");
mLinearLayout.addView(lEditText);
}
}
To get the values typed into the EditText, you need to additionally set an identifier for the view.
lEditText.setId(2); //you can use any integer ID
Then, you can retrieve the text inside the OnClickListener of the save button as:
EditText lEditText = (EditText)findViewById(2);
String txt = lEditText.getText().toString();
To create an edit text dynamically or programmatically:
EditText ed = new EditText(context);
Set whatever parameters you want to set for this edit text and then add this in your view:
view.addView(ed);
OR
view.addView(ed, layoutParams);
you can create EditText with the following code inside your Activity
EditText _edit = new EditText(this);
Then to add this to you activity layout you have to get the particular layout by it id For Ex.
LinearLayout linear = (LinearLayout)findViewById(R.id.linear);
then simple add this EditText object to the LinearLauout by using the following code..
linear.addView(_edit);
精彩评论