英文:
How create dynamic edittext from using api with input validation in android
问题
我需要从API在Android中生成动态输入字段。我还需要添加适当的输入验证,如不能为空、有效数字等,以及提交按钮。我尝试生成下面图片中的文本字段:
我的API JSON如下:
{
"fields": [
{
"code": "ACCT_NO",
"type": "textbox",
"required": true,
"default_value": null,
"options": null,
"regex": "^[0-9]*$",
"config": null,
"label": "Enter Account No:"
},
{
"code": "AMNT",
"type": "textbox",
"required": true,
"default_value": null,
"options": null,
"regex": "^[0-9]*$",
"config": null,
"label": "Enter Amount:"
},
{
"code": "ADD_MSISDN",
"type": "textbox",
"required": false,
"default_value": null,
"options": null,
"regex": null,
"config": null,
"label": "Additional MSISDN (Optional):"
}
]
}
以下是用于生成输入文本字段的XML代码(input_field_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:elevation="8dp"
android:background="#FFFFFF"
tools:ignore="MissingConstraints">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/textInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#9E9E9E"
android:maxLines="1"
android:inputType="text"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
以下是活动XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
以下是生成字段的代码:
fun populateFields(fields: List<Field>) {
val inflater = LayoutInflater.from(this)
for (x in fields) {
if (x.type == "textbox") {
val inputFieldLayout = inflater.inflate(R.layout.input_field_layout, containerLayout, false)
val textInputLayout = inputFieldLayout.findViewById<TextInputLayout>(R.id.textInputLayout)
val textInputEditText = inputFieldLayout.findViewById<TextInputEditText>(R.id.textInputEditText)
textInputEditText.inputType = InputType.TYPE_CLASS_TEXT
textInputLayout.hint = x.label
if (x.required) {
if (x.regex != null && x.regex != "") {
val regexPattern = x.regex
textInputEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
val value = s.toString()
val isValid = value.matches(Regex(regexPattern))
if (isValid) {
// 有效输入
textInputLayout.error = null
} else {
// 无效输入
textInputLayout.error = "无效输入"
}
}
})
}
}
containerLayout.addView(inputFieldLayout)
}
}
}
以下是提交按钮的代码:
btnNext.setOnClickListener {
Log.e("btnNext---","btnNext")
val fieldValues = mutableMapOf<Int, String>()
for (editText in editTextList) {
val value = editText.text.toString()
val fieldId = editText.id
fieldValues[fieldId] = value
Log.e("fieldId-",fieldId.toString())
Log.e("value-",value.toString())
}
}
如何将JSON中的字段ID设置为文本字段,该字段在JSON中定义为 "code": "ACCT_NO"?
如何在提交下一步按钮时验证输入字段是否为空或不为空以及其他验证?
英文:
I need to generate dynamic input fields from API in Android. I also need to add proper input validation submit button like not empty, valid number so on. I have tried to generate the text field below like image:
My API JSON is below:
fields": [
{
"code": "ACCT_NO",
"type": "textbox",
"required": true,
"default_value": null,
"options": null,
"regex": "^[0-9]*$",
"config": null,
"label": "Enter Account No:"
},
{
"code": "AMNT",
"type": "textbox",
"required": true,
"default_value": null,
"options": null,
"regex": "^[0-9]*$",
"config": null,
"label": "Enter Amount:"
},
{
"code": "ADD_MSISDN",
"type": "textbox",
"required": false,
"default_value": null,
"options": null,
"regex": null,
"config": null,
"label": "Additional MSISDN (Optional):"
}
]
Here is my XML code named input_field_layout.xml for generate input textfield:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:elevation="8dp"
android:background="#FFFFFF"
tools:ignore="MissingConstraints">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="8dp"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/textInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#9E9E9E"
android:maxLines="1"
android:inputType="text"
android:textSize="16sp" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my Activity XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/containerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Here is the fields generate code:
fun poplulateFields(fields: List<Field>){
val inflater = LayoutInflater.from(this)
for (x in fields) {
if (x.type.equals("textbox")){
val inputFieldLayout = inflater.inflate(R.layout.input_field_layout, containerLayout, false)
val textInputLayout = inputFieldLayout.findViewById<TextInputLayout>(R.id.textInputLayout)
val textInputEditText = inputFieldLayout.findViewById<TextInputEditText>(R.id.textInputEditText)
textInputEditText.inputType = InputType.TYPE_CLASS_TEXT
textInputLayout.hint = x.label
if(x.required){
if(x.regex != null || x.regex !=""){
val regexPattern = x.regex
textInputEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable?) {
val value = s.toString()
val isValid = value.matches(Regex(regexPattern))
if (isValid) {
// Valid input
textInputLayout.error = null
} else {
// Invalid input
textInputLayout.error = "Invalid input"
}
}
})
}
}
containerLayout.addView(inputFieldLayout)
// Add the EditText to the list for later use
editTextList.add(textInputEditText)
}
}
}
Here is my submit button code:
btnNext.setOnClickListener {
Log.e("btnNext---","btnNext")
val fieldValues = mutableMapOf<Int, String>()
for (editText in editTextList) {
val value = editText.text.toString()
val fieldId = editText.id
fieldValues[fieldId] = value
Log.e("fieldId-",fieldId.toString())
Log.e("value-",value.toString())
}
}
How to set the id from JSON to textfield which comes from json defined in "code": "ACCT_NO"?
how to validate input field not empty or not null or other validation on submit next button?
答案1
得分: 0
使用tag
来存储代码数据
val inputFieldLayout = inflater.inflate(R.layout.input_field_layout, containerLayout, false)
val textInputLayout = inputFieldLayout.findViewById<TextInputLayout>(R.id.textInputLayout)
val textInputEditText = inputFieldLayout.findViewById<TextInputEditText>(R.id.textInputEditText)
textInputEditText.inputType = InputType.TYPE_CLASS_TEXT
textInputEditText.tag = x.code // 将代码数据存储到编辑文本中
textInputLayout.hint = x.label
从tag
中检索代码数据
btnNext.setOnClickListener {
Log.e("btnNext---","btnNext")
val fieldValues = mutableMapOf<Int, String>()
for (editText in editTextList) {
val value = editText.text.toString()
val fieldId = editText.tag // 获取代码数据
fieldValues[fieldId] = value
Log.e("fieldId-",fieldId.toString())
Log.e("value-",value.toString())
}
}
要在单击下一步按钮时验证输入字段不为空,请使用验证函数和if语句。
// 验证所有字段都不为空
fun checkFieldsNotEmpty(editTextList: List<TextInputEditText>): Boolean {
for (editText in editTextList) {
if (editText.text.isNullOrEmpty()) return false
}
return true
}
btnNext.setOnClickListener {
// 如果任何编辑文本为空,则返回
if (!checkFieldsNotEmpty(editTextList)) return@setOnClickListener
Log.e("btnNext---","btnNext")
val fieldValues = mutableMapOf<Int, String>()
for (editText in editTextList) {
val value = editText.text.toString()
val fieldId = editText.id
fieldValues[fieldId] = value
Log.e("fieldId-",fieldId.toString())
Log.e("value-",value.toString())
}
}
英文:
Use tag
to store code data
val inputFieldLayout = inflater.inflate(R.layout.input_field_layout, containerLayout, false)
val textInputLayout = inputFieldLayout.findViewById<TextInputLayout>(R.id.textInputLayout)
val textInputEditText = inputFieldLayout.findViewById<TextInputEditText>(R.id.textInputEditText)
textInputEditText.inputType = InputType.TYPE_CLASS_TEXT
textInputEditText.tag = x.code // store code data into edit text
textInputLayout.hint = x.label
and retrieve code data from tag
btnNext.setOnClickListener {
Log.e("btnNext---","btnNext")
val fieldValues = mutableMapOf<Int, String>()
for (editText in editTextList) {
val value = editText.text.toString()
val fieldId = editText.tag // get code data
fieldValues[fieldId] = value
Log.e("fieldId-",fieldId.toString())
Log.e("value-",value.toString())
}
}
To validate input fields not empty on clicking the next button, use the validation function and if statement.
// validating all fields are not empty
fun checkFieldsNotEmpty(editTextList: List<TextInputEditText>): Boolean {
for (editText in editTextList) {
if (editText.text.isNullOrEmpty()) return false
}
return true
}
btnNext.setOnClickListener {
// any edit text is empty then return
if (!checkFieldsNotEmpty(editTextList)) return@setOnClickListener
Log.e("btnNext---","btnNext")
val fieldValues = mutableMapOf<Int, String>()
for (editText in editTextList) {
val value = editText.text.toString()
val fieldId = editText.id
fieldValues[fieldId] = value
Log.e("fieldId-",fieldId.toString())
Log.e("value-",value.toString())
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论