从生成的edittext android kotlin获取输入

bbuxkriu  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(470)

我正在制作一个随机词生成器,从kotlin中生成的edittext获取输入时遇到问题。我已经在java中找到了一些解决方案,我可以看到它们是如何工作的,但是我在将它放到kotlin中时遇到了困难。
我已经将它设置为while循环生成edittext,id存储在一个数组调用“arraylist”中。然后我想使用数组中的id从每个edittext中获取“text”,并将它们放入“strings”变量中。我认为在java中应该使用“string[i]”,这样变量就变成了string1、string2等,我不能让它工作。我试过打印数组和它的空白,所以我不认为我得到了正确的id。
代码中存在一些逻辑问题,比如已经有一个输入,我正在使用它来格式化从0开始的数组,这样我以后会整理出来。
谢谢,杰克

class WordList : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_word_list)

    //Get Linear layout as variable
    val linearLayout = findViewById(R.id.InfoLayout) as LinearLayout
    val Test = findViewById(R.id.WordsInput) as EditText
    val RandomiseButton = findViewById<Button>(R.id.RandomiseInputs) as Button
    var Value = "Hello" as String
    var editText = EditText (this)
    var List = arrayListOf<String>()
    var arraylist = ArrayList<Int>()
    val strings = ArrayList<String>()

    //Get Inputs from Previous page
    var Choice = intent.getIntExtra("Amount", 0)
    /*To Do
    Get Inputs From Created Inputs
    Randomise
    Print output
     */
    //Add new input
    if (Choice >= 2) {
        //Create Var for Edit
        var Number = 2

        //While loop to create multiple EditText fields
        while (Number <= Choice) {
            editText = EditText (this)
            editText.hint = "Input " + Number
            editText.setId(Number)
            //Use Appearance To change things you can't set using style.xml
            editText.setTextAppearance(R.style.TextHintFont)
            editText.setTextColor(Color.parseColor("#E321C2"))
            editText.setHintTextColor(Color.parseColor("#E321C2"))
            editText.setEms(10)
            //Set Edit
            linearLayout.addView(editText)
            arraylist.add(editText.id.toInt())
            Number++

        }

    }

    RandomiseButton.setOnClickListener {

        var Random = (0..Choice).random()

        var i = 0
        while (i <= arraylist.size) {
            strings.add(arraylist.get(i).text.toString())
            i++
        }

        var OutputW = strings.get(Random).toString()

            //Value = editText.text.toString()

        var intent = Intent (this@WordList,WordsOutput::class.java)
        intent.putExtra("RandomOut",OutputW)

        startActivity(intent)

    }

}
}
bxjv4tth

bxjv4tth1#

所以我就解决了

RandomiseButton.setOnClickListener {

        var Random = (0..Choice).random()

        var OutputW = linearLayout.getChildAt(Random) as EditText
        var another = OutputW.text.toString()

        var intent = Intent (this@WordList,WordsOutput::class.java)
        intent.putExtra("RandomOut",another)

        startActivity(intent)

}

我使用getchildat随机选择一个字段。更多信息请点击此处https://www.i-programmer.info/programming/android/11415-android-programming-in-kotlin-layouts-and-autonaming-components.html?start=1
只花了我3天哈哈哈

相关问题