android在单击(+)按钮时添加TextView,其中setText为1,在下一个2中依次为

时间:2017-04-07 14:02:24

标签: java android android-layout

我有一个textView和两个EditText,当我点击加号按钮时,应该添加textView和两个EditText,我能够做到。我的问题是当我添加TextView文本应该是设置为1然后再次添加TextView时,文本应设置为2,依此类推。我无法使用t1.setText(i)设置文本。这是我的代码 -

MainActivity.java



 import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.widget.Button;
    import android.widget.ImageButton;

    public class MainActivity extends Activity {
        Button btnDisplay;
        ImageButton btnAdd;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnAdd = (ImageButton) findViewById(R.id.btnAdd);
            btnDisplay = (Button) findViewById(R.id.btnDisplay);

            MyLayoutOperation.add(this, btnAdd);
            MyLayoutOperation.display(this, btnDisplay);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    }

 MyLayoutOperation.java

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MyLayoutOperation {

    public static void display(final Activity activity, Button btn)
    {
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
                java.util.ArrayList<String> msg = new ArrayList<String>();
                for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
                {
                    LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
  TextView t2=(TextView)innerLayout.findViewById(R.id.t1);
t2.setText(i);
                    EditText e1=(EditText)innerLayout.findViewById(R.id.e1);
                    EditText edit = (EditText) innerLayout.findViewById(R.id.editDes);
                    msg.add(t2.getText().toString());
                    msg.add(e1.getText().toString());
                    msg.add(edit.getText().toString());
                }
                Toast t = Toast.makeText(activity.getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT);
                t.show();
            }
        });
    }

    public static void add(final Activity activity, ImageButton btn)
    {
        final LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final LinearLayout newView = (LinearLayout)activity.getLayoutInflater().inflate(R.layout.rowdetail, null);
                newView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
                btnRemove.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        linearLayoutForm.removeView(newView);
                    }
                });
                linearLayoutForm.addView(newView);
            }
        });
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layoutTeste"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"

    tools:context=".MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView android:id="@+id/textView1"
            android:layout_width="95dp"
            android:layout_height="fill_parent"
            android:layout_alignBottom="@+id/btnAdd"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/btnAdd"
            android:gravity="center_vertical|center_horizontal"
            android:text="@string/titleTecnologies"

            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageButton android:id="@+id/btnAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:contentDescription="@string/btnAdd"
            android:src="@android:drawable/ic_input_add" />
    </RelativeLayout>
    <ScrollView android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="275dp">
        <LinearLayout android:id="@+id/linearLayoutForm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        </LinearLayout>
    </ScrollView>
    <Button android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnDisplay" />
</LinearLayout>

rowdetails.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rowdetail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:text="1"
        android:layout_marginLeft="10dp"
        android:id="@+id/t1"/> <requestFocus />
    <EditText android:id="@+id/e1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.62"
        android:ems="10"
        android:inputType="text"> <requestFocus />
    </EditText>
    <EditText android:id="@+id/editDes"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.62"
    android:ems="10"
    android:inputType="text"> <requestFocus />
</EditText>

    <ImageButton android:id="@+id/btnRemove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/btnRemove"
        android:src="@android:drawable/ic_delete" />
</LinearLayout>

1 个答案:

答案 0 :(得分:-1)

得到了答案

t2.setText(i+".");