Home > Net >  Kotlin Clearing edit text on click
Kotlin Clearing edit text on click

Time:10-03

So I am having issues finding a Kotlin way to clear a edit number field when the input is selected to enter a number.

  class MainActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //MyCode
        val submitButton: Button = findViewById(R.id.submit)
        var quarterQuan: EditText = findViewById(R.id.quarterInput)
        var dimeQuan: EditText = findViewById(R.id.dimeInput)
        val quarterEditTextValue = quarterQuan.text.toString().toFloat()
        val dimeEditTextValue = dimeQuan.text.toString().toFloat()
        val dime = Dime()
        val quarter= Quarter()
        var quarterTotal = quarter.quarterVal*quarterEditTextValue
        var dimeTotal = dime.dimeVal*dimeEditTextValue
        var cashTotal = dimeTotal   quarterTotal

    submitButton.setOnClickListener{
        var resultTextViewLabel: TextView = findViewById(R.id.textResult)
        resultTextViewLabel.text = cashTotal.toString()
    }
}

}

I tried to do something to this effect with no success....

       quarterQuan.setOnClickListener{
        editText.getText().clear();
    }

CodePudding user response:

in onCreate scope: first define a val(E.g. edTex) from your EditText(E.g. editTextU) then write a listener for it, to call cleartext() function.

       val edTex: EditText = findViewById(R.id.editTexU)
       edTex.setOnClickListener {cleartext()}
       //edTex.setOnFocusChangeListener {view, b -> cleartext()  }

Create toEditable() function for the String class. and write cleartext() function.

    fun String.toEditable(): Editable =  Editable.Factory.getInstance().newEditable(this)

  fun cleartext()
    {
       var str=""
       editTexU.text=str.toEditable()
    }

CodePudding user response:

An easy and uncomplicated way to clear the textview is by setting the text to an emty string:

editText.setText("")
  • Related