动态形式与重复形式

编程入门 行业动态 更新时间:2024-10-27 15:19:16
本文介绍了动态形式与重复形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在android中创建一个必须具有 form 的页面,该页面会根据某些值动态重复.

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.

我如何实现这样的目标?我可以使用listview或recyclerview吗?如果是这样,那我将如何检索键入的值?

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:,您应该使用 recyclerview

我如何实现这样的目标?

How can I achieve something like this?

创建与recyclerview单个项目类似的布局

Create a layout like that for recyclerview single item

我将如何检索键入的值?

how will I have to retrieve the typed in values?

您可以从 RecyclerView.Adapter 类

You can get values from RecyclerView.Adapter class

HERE IS THE SAMPLE CODE

HERE IS THE SAMPLE CODE

Activity.java

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

Activity.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" xmlns:tools="schemas.android/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课

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

适配器类

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="schemas.android/apk/res/android" xmlns:app="schemas.android/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>

更多推荐

动态形式与重复形式

本文发布于:2023-10-07 17:12:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469988.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:形式   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!