mardi 1 mai 2018

kotlin if statement sending data if empty

Hello i have my code that somewhat works when there is no input to the text box it still sends the data i'm unsure how i put more than one if statement together so if any of the three text boxes are empty it wont let me send the data across to be displayed

class MainActivity : AppCompatActivity() {

    var editText: EditText? = null
    var editText2: EditText? = null;
    var editText3 : EditText ?= null;

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

        editText = findViewById<EditText>(R.id.editText)
        editText2 = findViewById<EditText>(R.id.editText2)
        editText3 = findViewById<EditText>(R.id.editText3)
    }



    /** Called when the user taps the Send button */
    fun sendMessage(view: View) {


        if (editText!!.text.toString().length == 0)
            editText!!.error = "First name is required!"

        if (editText2!!.text.toString().length == 0)
            editText2!!.error = "Email is required"

        if (editText3!!.text.toString().length == 0)
            editText3!!.error = "Compnay name is required!"

        val message1 = editText!!.text.toString()
        val message2 = editText2!!.text.toString()
        val message3 = editText3!!.text.toString()

        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
            putExtra("EXTRA_MSG1", message1)
            putExtra("EXTRA_MSG2", message2)
            putExtra("EXTRA_MSG3", message3)

        }

        startActivity(intent)
    }
} 

Aucun commentaire:

Enregistrer un commentaire