我是新手。
我想在用户点击按钮时添加3个EditText。
我试图意识到这一点,但这不起作用。
小部件不会出现。
P.S。小部件必须位于其他布局中。
谢谢你的帮助。 抱歉我的英语不好。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_fill"
android:id="@+id/simple_main_layout">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/for_main_text">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Buy List"
android:id="@+id/textView" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/do_new_list"
android:layout_marginLeft="153dp"
android:layout_marginRight="2dp"
android:background="@drawable/btn_yes" android:textColor="#804f29" android:textStyle="bold"/>/>
</LinearLayout>
<EditText
android:layout_width="305dp"
android:layout_height="wrap_content"
android:id="@+id/editText" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="91dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText3" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
代码:
private Button do_new_field;
private EditText[] text, many, cost;
private final int fields = 50;
private int last_field = 0;
private LinearLayout layoutik;
LinearLayout.LayoutParams layoutParams;
LinearLayout.LayoutParams layoutParams2;
@Override
public void onBackPressed() {
super.onBackPressed();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_buy);
do_new_field = (Button) findViewById(R.id.do_new_list);
layoutik = (LinearLayout) findViewById(R.id.for_main_text);
do_new_field.setOnClickListener(this);
text = new EditText[fields];
many = new EditText[fields];
cost = new EditText[fields];
layoutParams =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,0f);
layoutParams2 =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT,0f);
}
@Override
public void onClick(View view) {
if(view == do_new_field)
{
if(last_field <= fields)
{
text[last_field] = new EditText(this);
many[last_field] = new EditText(this);
cost[last_field] = new EditText(this);
text[last_field].setWidth(305);
text[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
text[last_field].setMaxLines(3);
many[last_field].setWidth(91);
many[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
many[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
many[last_field].setMaxLines(1);
cost[last_field].setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
cost[last_field].setInputType(InputType.TYPE_CLASS_NUMBER);
cost[last_field].setMaxLines(1);
text[last_field].setId(100 + last_field);
many[last_field].setId(200 + last_field);
cost[last_field].setId(300 + last_field);
text[last_field].setLayoutParams(layoutParams);
many[last_field].setLayoutParams(layoutParams);
cost[last_field].setLayoutParams(layoutParams);
layoutik.addView(text[last_field]);
layoutik.addView(many[last_field]);
layoutik.addView(cost[last_field]);
last_field++;
}
}
} }
答案 0 :(得分:0)
You might have to add "setId" to the editText and check.
TableLayout.LayoutParams params = new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0f);
editText.setId(EDT_ID + sub_count + i);
editText.setTag(insertAdAl.get(i).getmApiAppend());
editText.setLayoutParams(params);
editText.setTextAppearance(NewAdverts.this,
android.R.style.TextAppearance_Small);
editText.setGravity(Gravity.LEFT | Gravity.CENTER);
editText.setPadding(4, 4, 4, 4);
editText.setTextAppearance(NewAdverts.this,
android.R.attr.textAppearanceSmall);
editText.setTextColor(Color.BLACK);
editText.setBackgroundResource(R.drawable.notification_field);