mercredi 6 janvier 2021

Getting Input From Generated EditText Android Kotlin

I'm making a random word generator and I'm having problems getting the input from the generated editText in Kotlin. I have found a few solutions in java and I can see how they work but I'm having trouble putting it into Kotlin.

I've set it up the so the EditTexts are generated by a while loop and the Id is stored in an array call "arraylist". I then wanted to use the Id in the array to obtain the "text" from each editText and put them into the "Strings" variable. I think in java you'd use "string[i]" so the variable becomes string1, string2 etc. I can't get this to work. I've tried printing the array and its blank so I don't think I'm getting the id correctly.

There are a few logic issues with code such as there already being an input that I'm using for formatting and arrays starting at 0 and such that I'll sort out later.

Thanks Jake

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)


    }

}
}



Aucun commentaire:

Enregistrer un commentaire