Kotlin 在动态添加的编辑文本上设置属性
Kotlin set attributes on a dynamically added edittext
我正在尝试学习 Kotlin,我想创建多个 EditText 动态地彼此相邻放置在 android phone.
EditText 的数量取决于用户必须猜测的单词长度。因此,如果单词是 dog,则应向用户显示单词中每个字母的 EditText。
所以我做了这样的事情。
for (letter in word) {
val editText = EditText(this)
...
}
然后将它们附加到线性布局。
但是我想在附加它之前在编辑文本上设置一些 属性,为此我完全迷路了。
谁能给我指出正确的方向。
请帮助我,我很沮丧...:-)
如果您知道要将哪些属性添加到编辑文本中,您可以为其创建一个 XML 布局。您可以设置文本颜色、背景等,然后展开布局。
所以在你的 for 循环中你会做这样的事情
val inflater = LayoutInflater.from(this)
val editText = inflater.inflate(R.layout.YourCustomEditText, null, false) as EditText
// add to editText to your linearLayout as you're already doing
然后 YouCustomEditText 将是这样的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_text_vew"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="@color/white"
android:hint="hint"
android:orientation="vertical"
android:padding="4dp"
android:paddingStart="8dp"
android:paddingTop="4dp"
android:paddingEnd="8dp"
android:textColor="@color/colorAccent"></EditText>
使用 EditText 创建 xml 布局
input_edittext_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/input_letter_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp"
/>
在您想要使用 id 显示的主布局中添加一个 LinearLayout
activity_words.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".WordsActivity">
<LinearLayout
android:id="@+id/letter_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
</LinearLayout>
WordsActivity.kt
class WordsActivity : AppCompatActivity() {
val wordlist = listOf("Cat", "dog", "Tiger", "Elephant")
lateinit var wordlayout: LinearLayout
val currentEditTextList = mutableListOf<EditText>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_words)
wordlayout = findViewById<LinearLayout>(R.id.letter_input_layout)
findMatchedString("Elephant")//example calling from here
}
//String passed as argument to compare from word list
fun findMatchedString(inputString: String){
wordlist.forEach {
if(it == inputString){
addEditTextToLayout(inputString)
}else{
//Handle your logic to handle else case
}
}
}
fun addEditTextToLayout(matchedString: String) {
//first adding EditText objects to list
matchedString?.let {
it.forEachIndexed { index, element ->
val editText = getInputEditText()
editText.tag = index
editText.setText(element.toString())//This line added to check the
each letter on the each EditText separately
currentEditTextList.add(editText)
}
}
//second adding each EditText from list to layout
currentEditTextList?.let {
if (it.size > 0) {
it.forEachIndexed { index, element ->
element.tag = index
wordlayout.addView(element)
}
}
}
}
fun getInputEditText() : EditText{
return layoutInflater.inflate(R.layout.input_edittext_layout, null, false) as
EditText
}
}
我正在尝试学习 Kotlin,我想创建多个 EditText 动态地彼此相邻放置在 android phone.
EditText 的数量取决于用户必须猜测的单词长度。因此,如果单词是 dog,则应向用户显示单词中每个字母的 EditText。
所以我做了这样的事情。
for (letter in word) {
val editText = EditText(this)
...
}
然后将它们附加到线性布局。 但是我想在附加它之前在编辑文本上设置一些 属性,为此我完全迷路了。
谁能给我指出正确的方向。
请帮助我,我很沮丧...:-)
如果您知道要将哪些属性添加到编辑文本中,您可以为其创建一个 XML 布局。您可以设置文本颜色、背景等,然后展开布局。
所以在你的 for 循环中你会做这样的事情
val inflater = LayoutInflater.from(this)
val editText = inflater.inflate(R.layout.YourCustomEditText, null, false) as EditText
// add to editText to your linearLayout as you're already doing
然后 YouCustomEditText 将是这样的 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_text_vew"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="@color/white"
android:hint="hint"
android:orientation="vertical"
android:padding="4dp"
android:paddingStart="8dp"
android:paddingTop="4dp"
android:paddingEnd="8dp"
android:textColor="@color/colorAccent"></EditText>
使用 EditText 创建 xml 布局
input_edittext_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/input_letter_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp"
/>
在您想要使用 id 显示的主布局中添加一个 LinearLayout
activity_words.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".WordsActivity">
<LinearLayout
android:id="@+id/letter_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
</LinearLayout>
WordsActivity.kt
class WordsActivity : AppCompatActivity() {
val wordlist = listOf("Cat", "dog", "Tiger", "Elephant")
lateinit var wordlayout: LinearLayout
val currentEditTextList = mutableListOf<EditText>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_words)
wordlayout = findViewById<LinearLayout>(R.id.letter_input_layout)
findMatchedString("Elephant")//example calling from here
}
//String passed as argument to compare from word list
fun findMatchedString(inputString: String){
wordlist.forEach {
if(it == inputString){
addEditTextToLayout(inputString)
}else{
//Handle your logic to handle else case
}
}
}
fun addEditTextToLayout(matchedString: String) {
//first adding EditText objects to list
matchedString?.let {
it.forEachIndexed { index, element ->
val editText = getInputEditText()
editText.tag = index
editText.setText(element.toString())//This line added to check the
each letter on the each EditText separately
currentEditTextList.add(editText)
}
}
//second adding each EditText from list to layout
currentEditTextList?.let {
if (it.size > 0) {
it.forEachIndexed { index, element ->
element.tag = index
wordlayout.addView(element)
}
}
}
}
fun getInputEditText() : EditText{
return layoutInflater.inflate(R.layout.input_edittext_layout, null, false) as
EditText
}
}