按钮上的动态编辑文本添加和删除

时间:2018-06-12 07:01:54

标签: android

我已经在这里或其他任何地方阅读过代码。我真的很困惑。我发现在web上简单的编程添加和删除edittext与删除按钮。但是当我添加更多文本视图时,它具有相同的ID,但我需要该文本内部写的内容。如果可能的话我需要下一个带字母的id,而不是数字。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    private LinearLayout parentLinearLayout1;
    private LinearLayout parentLinearLayout2;
    TextView a1;
    TextView txt;

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        parentLinearLayout1 = (LinearLayout) findViewById (R.id.parent_linear_layout1);
        parentLinearLayout2 = (LinearLayout) findViewById (R.id.parent_linear_layout2);
        a1 = (TextView) findViewById (R.id.textView5);
        txt = (EditText) findViewById (R.id.txt);
    }

    public void onAddField( View v ) {
        LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate (R.layout.field, null);
        parentLinearLayout1.addView (rowView, parentLinearLayout1.getChildCount () );
        Button addbtn = (Button) findViewById (R.id.add_field_button);

        int id = addbtn.getId ();
        a1 = (TextView) findViewById (R.id.textView5);
        a1.setText (id + "");
    }

    public void onDelete( View v ) {
        parentLinearLayout1.removeView ((View) v.getParent ());
        Button del = (Button) findViewById (R.id.deletebuton1);
        // c=(EditText)findViewById (R.id.number_edit_text);
        int id = del.getId ();
        a1 = (TextView) findViewById (R.id.textView5);
        a1.setText (id + "");
    }

}

activity_main xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Město" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parent_linear_layout1"
        android:layout_width="374dp"
        android:layout_height="120dp"
        android:layout_margin="5dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/number_edit_text"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="5"
                android:inputType="text" />

            <Button
                android:id="@+id/add_field_button"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:background="#555"
                android:onClick="onAddField"
                android:paddingLeft="5dp"
                android:text="Add Field"
                android:textColor="#FFF" />

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

和字段xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/number_edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:inputType="text" />

    <Button
        android:id="@+id/deletebuton1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:drawable/ic_delete"
        android:onClick="onDelete">

</LinearLayout>

它甚至可能或者这个代码是无意义的,我必须重新开始? 谢谢你的回答。

0 个答案:

没有答案