答案 0 :(得分:2)
只需创建一个新的 EditText(查看),然后将其添加到首选位置的布局。
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayoutDecisions);
EditText editText = new EditText(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(p);
linearLayout.addView(editText);
答案 1 :(得分:1)
此代码可以帮助您
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Edit Text" >
</Button>
</LinearLayout>
<强> MainActivity.java 强>
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private Button buttonView;
private LinearLayout parentLayout;
private int hint=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonView=(Button)findViewById(R.id.buttonView);
parentLayout = (LinearLayout)findViewById(R.id.parentLayout);
buttonView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
createEditTextView();
}
});
}
protected void createEditTextView() {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.setMargins(0,10,0,10);
EditText edittTxt = new EditText(this);
int maxLength = 5;
hint++;
edittTxt.setHint("editText"+hint);
edittTxt.setLayoutParams(params);
// edtTxt.setBackgroundColor(Color.WHITE);
edittTxt.setInputType(InputType.TYPE_CLASS_TEXT);
edittTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP,18);
edittTxt.setId(hint);
InputFilter[] fArray = new InputFilter[1];
fArray[0] = new InputFilter.LengthFilter(maxLength);
edittTxt.setFilters(fArray);
parentLayout.addView(edittTxt);
}
}
答案 2 :(得分:0)
使用TableLayout和TableRow。您可以非常轻松地动态添加和删除。
This可能是一个很好的起点