简体   繁体   中英

Android: Mistake with adding widgets

I'm a newbie.

I want to add 3 EditText's when a user click on a button.

I try to realize it, but it isn't work.

Widgets do not appear.

PS Widgets must be in others layout's.

Thank you for help. Sorry for my bad English.

 <?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>

Code:

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++;
    }
}

} }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM