文本编辑中的行下降 Android Studio
Line drop in text editing Android Studio
关于如何让 EditText
接收用户输入大约 9 个字母(或数字)并在完成后(例如:单击某些按钮操作或失去键盘焦点)的任何建议,它将更新其中的字母EditText
。以下是要求:
输入:123456789
输出:
123
456
789
enter image description here
请确认这是否是您想要实现的目标?
There's EditText
, you want that EditText
able to add newline (multiline) for every 3 character (after a simple actions)
如果是,这里有一个可能解决潜在问题的自以为是的解决方案:
以上截图写在here
对于EditText
部分,目前我们能想到的:
See Core-KTX extensions from here
// YourActivity.kt
import androidx.core.widget.doAfterTextChanged
import kotlin.text.buildString // this import is optional. to identify its origin
override fun onCreate(...) {
// assign your `editText` & `textView` variable with usual `findViewById` or using `ViewBinding` if your project already using it
// Directly listen for user input in EditText
editText?.doAfterTextChanged { userInput ->
if (userInput.length == 3) textView?.text = "$userInput\n"
}
// Or you can use the below approach:
textView.text = buildString {
editText?.toString()?.forEachIndexed { index, letter ->
append(c)
// index start from 0
// index+1 = 0+1, so we can start from 1-index
// check for the reminder of index/3 == 0
// meaning we are appending the `\n` (newline) to the text
if ((index+1) % 3 == 0) append("\n")
}
}
}
// your_activity.xml
<LinearLayout
...
<EditText ... id="@id/editText" />
// create below TextView as a result of user inputs
<TextView ... id="@id/textView" />
/>
A few lines in the above snippet code omitted for readability, and yes there's a few code will compile-error as well, needs to adjust it accordingly
关于如何让 EditText
接收用户输入大约 9 个字母(或数字)并在完成后(例如:单击某些按钮操作或失去键盘焦点)的任何建议,它将更新其中的字母EditText
。以下是要求:
输入:123456789
输出:
123
456
789
enter image description here
请确认这是否是您想要实现的目标?
There's
EditText
, you want thatEditText
able to add newline (multiline) for every 3 character (after a simple actions)
如果是,这里有一个可能解决潜在问题的自以为是的解决方案:
以上截图写在here
对于EditText
部分,目前我们能想到的:
See Core-KTX extensions from here
// YourActivity.kt
import androidx.core.widget.doAfterTextChanged
import kotlin.text.buildString // this import is optional. to identify its origin
override fun onCreate(...) {
// assign your `editText` & `textView` variable with usual `findViewById` or using `ViewBinding` if your project already using it
// Directly listen for user input in EditText
editText?.doAfterTextChanged { userInput ->
if (userInput.length == 3) textView?.text = "$userInput\n"
}
// Or you can use the below approach:
textView.text = buildString {
editText?.toString()?.forEachIndexed { index, letter ->
append(c)
// index start from 0
// index+1 = 0+1, so we can start from 1-index
// check for the reminder of index/3 == 0
// meaning we are appending the `\n` (newline) to the text
if ((index+1) % 3 == 0) append("\n")
}
}
}
// your_activity.xml
<LinearLayout
...
<EditText ... id="@id/editText" />
// create below TextView as a result of user inputs
<TextView ... id="@id/textView" />
/>
A few lines in the above snippet code omitted for readability, and yes there's a few code will compile-error as well, needs to adjust it accordingly