如何在android中逐步添加数据?
Posted
技术标签:
【中文标题】如何在android中逐步添加数据?【英文标题】:How to add data step by step in android? 【发布时间】:2021-12-21 09:58:43 【问题描述】:我想通过 kotlin 语言 来实现这个添加。分步:
-
对于每个需要添加的数据,用户点击
+Add other deposit
,代表第一个允许添加新数据的区域,+Add other deposit
按钮向下。
如果用户想取消添加功能,只需点击×
按钮取消,+Add other deposit
按钮再次返回。
EditText
中输入的数据和RadioButton
中选择的选项被保存在变量中并由FloatingActionButton
提交。
非常感谢您提前做出的努力
【问题讨论】:
伙计们。你想发布我做过的 kotlin 代码并标记代码供我自己尝试吗? 【参考方案1】:这可以通过创建一个列表来完成,当您单击 +添加其他存款按钮时,您可以使用 recyclerview 适配器将新项目添加到列表中。输入字段的一个问题,我在用户按下键盘上的完成按钮时保存信息,也可以在失去焦点时保存字段中的数据,决定按下后保存更好。
package com.myply.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnAddOtherDeposit = findViewById<TextView>(R.id.btn_add_other_deposit)
val rvCustomers = findViewById<RecyclerView>(R.id.rv_customers)
val fabDone = findViewById<FloatingActionButton>(R.id.fab_done)
val adapter = MyRecyclerAdapter(this, arrayListOf(CustomerModel()))
rvCustomers.adapter = adapter
rvCustomers.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
adapter.mClickListener = object : MyRecyclerAdapter.ItemClickListener
override fun onItemRemoveClicked(position: Int)
adapter.removeAt(position)
btnAddOtherDeposit.setOnClickListener
/*add empty model without information */
adapter.add(CustomerModel())
fabDone.setOnClickListener
/*collect all data*/
var customers = adapter.data
适配器
package com.myply.myapplication
import android.content.Context
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class MyRecyclerAdapter internal constructor(
val context: Context?,
val data: MutableList<CustomerModel>
) : RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>()
var mClickListener: ItemClickListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder
val view = LayoutInflater.from(context).inflate(R.layout.item_customer, parent, false)
return ViewHolder(view)
override fun onBindViewHolder(holder: ViewHolder, position: Int)
val customer = data[position]
holder.editName.setText(customer.name)
holder.rbDeposit.setOnCheckedChangeListener(null)
holder.rbCheque.setOnCheckedChangeListener(null)
holder.rbDeposit.isChecked = customer.depositType == "Deposit"
holder.rbCheque.isChecked = customer.depositType == "Cheque"
holder.btnRemove.setOnClickListener mClickListener?.onItemRemoveClicked(position)
holder.editName.setOnEditorActionListener(object : TextView.OnEditorActionListener
override fun onEditorAction(p0: TextView?, actionId: Int, event: KeyEvent?): Boolean
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event?.action == KeyEvent.ACTION_DOWN && event?.keyCode == KeyEvent.KEYCODE_ENTER)
customer.name = holder.editName.text.toString()
holder.editName.clearFocus()
update(customer, position)
return true
return false
)
holder.rbDeposit.setOnCheckedChangeListener compoundButton, b ->
customer.depositType = "Deposit"
update(customer, position)
holder.rbCheque.setOnCheckedChangeListener compoundButton, b ->
customer.depositType = "Cheque"
update(customer, position)
override fun getItemCount(): Int
return data.size
fun add(customer: CustomerModel)
data.add(customer)
notifyItemInserted(data.size - 1)
fun update(customer: CustomerModel, position: Int)
data[position] = customer
notifyItemChanged(position)
fun removeAt(position: Int)
data.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, data.size)
inner class ViewHolder internal constructor(itemView: View) :
RecyclerView.ViewHolder(itemView)
var editName: EditText = itemView.findViewById(R.id.edit_name)
var btnRemove: ImageView = itemView.findViewById(R.id.btn_remove)
var rbDeposit: RadioButton = itemView.findViewById(R.id.rb_deposit)
var rbCheque: RadioButton = itemView.findViewById(R.id.rb_cheque)
interface ItemClickListener
fun onItemRemoveClicked(position: Int)
item_customer.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"
android:layout_
android:layout_>
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/edit_name"
android:layout_
android:layout_
android:imeOptions="actionDone"
android:singleLine="true" />
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_deposit"
android:layout_
android:layout_
android:text="Deposit type" />
<RadioButton
android:id="@+id/rb_cheque"
android:layout_
android:layout_
android:text="Cheque" />
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/btn_remove"
android:layout_
android:layout_
android:layout_alignParentEnd="true"
app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" />
</RelativeLayout>
activity_main.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_
android:layout_
tools:context=".MainActivity">
<androidx.core.widget.NestedScrollView 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_
android:layout_
tools:context=".MainActivity">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_customers"
android:layout_
android:layout_ />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/btn_add_other_deposit"
android:layout_
android:layout_
android:layout_gravity="end"
android:text="Add other deposit" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_done"
android:layout_
android:layout_
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp" />
</RelativeLayout>
【讨论】:
首先,我的语言和打字无法形容我多么感谢您为完成这项工作所做的努力。但我的经历不像你。有几点想问你。所以,请提供给我一个联系方式来讨论更多关于这些要点 @MohamadMF 如果您有t.me/yura_yatsko,请在电报中给我写信以上是关于如何在android中逐步添加数据?的主要内容,如果未能解决你的问题,请参考以下文章