如何解决Kotlin在动态添加的edittext上设置属性
我正在尝试学习Kotlin,我想创建许多 EditText ,它们动态地彼此并排放置在Android手机的屏幕上。
EditText 的数量取决于用户必须猜测的单词长度。因此,如果单词是 dog ,则该单词中的每个字母都应向用户显示 EditText 。
所以我做了这样的事情。
for (letter in word) {
val editText = EditText(this)
...
}
,然后将它们附加到线性布局。 但是我想在添加编辑文本之前为其添加一些属性,因此我完全迷失了。
有人能指出我正确的方向吗?
请帮助我,我非常沮丧...:-)
解决方法
如果知道要添加到编辑文本中的属性,则可以为其创建XML布局。您可以设置textColor,background等,然后为布局充气。
因此在您的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,false) as
EditText
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。