具有重复形式的动态形式 [英] Dynamic form with repeating form

查看:20
本文介绍了具有重复形式的动态形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 android 中创建一个页面,该页面必须有一个 表单,该页面将根据某些值动态重复.

<块引用>

像这样:

这里的表单用户名、密码、手机和地址字段根据用户在此之前在页面中输入的数字重复.在这张图片中,表单重复了两次,但它可以是动态的任意次数.

我怎样才能实现这样的目标?我可以使用 listviewrecyclerview 吗?如果是这样,那么我将如何检索输入的值?

解决方案

这里的表单用户名、密码、手机和地址字段根据用户在此之前在页面中输入的数字重复.在这张图片中,表格重复了两次,但它可以是任意次数,这将是动态的

ANS :你应该使用 recyclerview

<块引用>

我怎样才能实现这样的目标?

recyclerview 单个项目创建一个这样的布局

<块引用>

我将如何检索输入的值?

您可以从RecyclerView.Adapter

获取值

这是示例代码

<块引用>

Activity.java

公共类 AddmoreActivity 扩展 AppCompatActivity {RecyclerView myRc;ArrayListarrayList = 新的 ArrayList<>();按钮 btnGetData;添加更多适配器适配器;@覆盖protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_addmore);myRc = (RecyclerView) findViewById(R.id.myRc);btnGetData = (Button) findViewById(R.id.btnGetData);myRc.setHasFixedSize(true);myRc.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));AddMorePojo addMorePojo = new AddMorePojo();addMorePojo.setAddress("");addMorePojo.setUserName("");arrayList.add(addMorePojo);适配器 = 新的 AddMoreAdapter(this, arrayList);myRc.setAdapter(适配器);btnGetData.setOnClickListener(new View.OnClickListener() {@覆盖公共无效onClick(查看视图){ArrayListpojoArrayList = adapter.getArrayList();for (int i = 0; i < pojoArrayList.size(); i++) {Log.e("Name " + i, pojoArrayList.get(i).getUserName() + "");Log.e("Pass" + i, pojoArrayList.get(i).getPass() + "");Log.e("PHONE " + i, pojoArrayList.get(i).getPhone() + "");Log.e("地址" + i, pojoArrayList.get(i).getAddress() + "");}}});}}

<块引用>

Activity.xml

<android.support.v7.widget.RecyclerViewandroid:id="@+id/myRc"android:layout_width="match_parent"android:layout_height="wrap_content"/><按钮android:id="@+id/btnGetData"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:text="获取数据"/></RelativeLayout>

<块引用>

pojo 类

公共类 AddMorePojo {字符串用户名、电话、通行证、地址;公共 AddMorePojo() {}公共字符串 getUserName() {返回用户名;}public void setUserName(String userName) {this.userName = 用户名;}公共字符串 getPhone() {回电话;}公共无效setPhone(字符串电话){this.phone = 电话;}公共字符串 getPass() {回程通行证;}公共无效setPass(字符串传递){this.pass = 通过;}公共字符串 getAddress() {退货地址;}公共无效集地址(字符串地址){地址 = 地址;}}

<块引用>

适配器类

public class AddMoreAdapter extends RecyclerView.Adapter{上下文上下文;ArrayList数组列表;public AddMoreAdapter(上下文上下文,ArrayListarrayList){this.context = 上下文;this.arrayList = arrayList;}@覆盖public AddMoreAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {视图视图 = LayoutInflater.from(context).inflate(R.layout.addmorelayout, parent, false);返回新的 ViewHolder(view);}@覆盖public void onBindViewHolder(AddMoreAdapter.ViewHolder holder, int position) {}@覆盖公共 int getItemCount() {返回 arrayList.size();}公共 ArrayList获取数组列表(){返回数组列表;}公共类 ViewHolder 扩展 RecyclerView.ViewHolder {EditText edtName、edtPhone、edtPass、edtAdrress;公共 ViewHolder(查看 itemView){超级(项目视图);edtName = itemView.findViewById(R.id.edtUname);edtPhone = itemView.findViewById(R.id.edtPhone);edtPass = itemView.findViewById(R.id.edtPass);edtAdrress = itemView.findViewById(R.id.edtAddress);edtName.addTextChangedListener(new TextWatcher() {@覆盖public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@覆盖public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());addMorePOJO.setUserName(charSequence + "");arrayList.set(getAdapterPosition(), addMorePOJO);}@覆盖public void afterTextChanged(可编辑可编辑){}});edtPhone.addTextChangedListener(new TextWatcher() {@覆盖public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@覆盖public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());addMorePOJO.setPhone(charSequence + "");arrayList.set(getAdapterPosition(), addMorePOJO);}@覆盖public void afterTextChanged(可编辑可编辑){}});edtPass.addTextChangedListener(new TextWatcher() {@覆盖public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@覆盖public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());addMorePOJO.setPass(charSequence + "");arrayList.set(getAdapterPosition(), addMorePOJO);}@覆盖public void afterTextChanged(可编辑可编辑){}});edtAdrress.addTextChangedListener(new TextWatcher() {@覆盖public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}@覆盖public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());addMorePOJO.setAddress(charSequence + "");arrayList.set(getAdapterPosition(), addMorePOJO);}@覆盖public void afterTextChanged(可编辑可编辑){}});}}}

<块引用>

适配器自定义布局

<线性布局android:layout_width="match_parent"android:layout_height="wrap_content"机器人:方向=垂直"><编辑文本android:id="@+id/edtUname"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入用户名"/><编辑文本android:id="@+id/edtPass"android:layout_width="match_parent"android:layout_height="wrap_content"机器人:提示=********"android:imeOptions="actionNext"android:inputType="textPassword"/><编辑文本android:id="@+id/edtPhone"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入电话号码"android:imeOptions="actionNext"android:inputType="numberDecimal"/><编辑文本android:id="@+id/edtAddress"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="输入地址"android:imeOptions="actionNext"android:inputType="文本"/></LinearLayout></android.support.v7.widget.CardView>

I would like to create a page in android which has to have a form which will repeat based on some value dynamically.

Something like this:

Here form username, password, mobile and address fields are repeated based on number user enters in the the page before this. In this picture the form is repeated twice but it could be any number of times which will be dynamic.

How can I achieve something like this? Can I use listview or recyclerview? If so then how will I have to retrieve the typed in values?

解决方案

Here form username, password, mobile and address fields are repeated based on number user enters in the the page before this. In this picture the form is repeated twice but it could be any number of times which will be dynamic

ANS : you should go with recyclerview

How can I achieve something like this?

Create a layout like that for recyclerview single item

how will I have to retrieve the typed in values?

You can get values from RecyclerView.Adapter class

HERE IS THE SAMPLE CODE

Activity.java

public class AddmoreActivity extends AppCompatActivity {

    RecyclerView myRc;
    ArrayList<AddMorePojo> arrayList = new ArrayList<>();
    Button btnGetData;
    AddMoreAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addmore);

        myRc = (RecyclerView) findViewById(R.id.myRc);
        btnGetData = (Button) findViewById(R.id.btnGetData);

        myRc.setHasFixedSize(true);
        myRc.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        AddMorePojo addMorePojo = new AddMorePojo();
        addMorePojo.setAddress("");
        addMorePojo.setUserName("");
        arrayList.add(addMorePojo);


        adapter = new AddMoreAdapter(this, arrayList);
        myRc.setAdapter(adapter);

        btnGetData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<AddMorePojo> pojoArrayList = adapter.getArrayList();

                for (int i = 0; i < pojoArrayList.size(); i++) {

                    Log.e("Name " + i, pojoArrayList.get(i).getUserName() + "");
                    Log.e("Pass " + i, pojoArrayList.get(i).getPass() + "");
                    Log.e("PHONE " + i, pojoArrayList.get(i).getPhone() + "");
                    Log.e("Address " + i, pojoArrayList.get(i).getAddress() + "");
                }
            }
        });

    }
}

Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.user33.workingtestapp.AddmoreActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/myRc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <Button
        android:id="@+id/btnGetData"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="get Data" />
</RelativeLayout>

pojo class

public class AddMorePojo {

    String userName, phone, pass, Address;

    public AddMorePojo() {
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String address) {
        Address = address;
    }
}

adapter class

public class AddMoreAdapter extends RecyclerView.Adapter<AddMoreAdapter.ViewHolder> {
    Context context;
    ArrayList<AddMorePojo> arrayList;

    public AddMoreAdapter(Context context, ArrayList<AddMorePojo> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public AddMoreAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.addmorelayout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(AddMoreAdapter.ViewHolder holder, int position) {

    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public ArrayList<AddMorePojo> getArrayList()
    {
        return arrayList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        EditText edtName, edtPhone, edtPass, edtAdrress;

        public ViewHolder(View itemView) {
            super(itemView);

            edtName = itemView.findViewById(R.id.edtUname);
            edtPhone = itemView.findViewById(R.id.edtPhone);
            edtPass = itemView.findViewById(R.id.edtPass);
            edtAdrress = itemView.findViewById(R.id.edtAddress);

            edtName.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());
                    addMorePOJO.setUserName(charSequence + "");
                    arrayList.set(getAdapterPosition(), addMorePOJO);


                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            edtPhone.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());
                    addMorePOJO.setPhone(charSequence + "");
                    arrayList.set(getAdapterPosition(), addMorePOJO);


                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });
            edtPass.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());
                    addMorePOJO.setPass(charSequence + "");
                    arrayList.set(getAdapterPosition(), addMorePOJO);


                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            edtAdrress.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    AddMorePojo addMorePOJO = arrayList.get(getAdapterPosition());
                    addMorePOJO.setAddress(charSequence + "");
                    arrayList.set(getAdapterPosition(), addMorePOJO);


                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });


        }
    }
}

adapter custom layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/edtUname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter User Name" />

        <EditText
            android:id="@+id/edtPass"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="********"
            android:imeOptions="actionNext"
            android:inputType="textPassword" />


        <EditText
            android:id="@+id/edtPhone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Phone Number"
            android:imeOptions="actionNext"
            android:inputType="numberDecimal" />

        <EditText
            android:id="@+id/edtAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Address"
            android:imeOptions="actionNext"
            android:inputType="text" />


    </LinearLayout>

</android.support.v7.widget.CardView>

这篇关于具有重复形式的动态形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆