mardi 1 mai 2018

kotlin if else statement

I currently have a working if statement but if the user inputs any of the three fields they can send the data how do i get a if else statement to work so unless all three fields are filled you cannot send the data.

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) {
            Toast.makeText(this, "You did not enter your name", Toast.LENGTH_SHORT).show();

            if (editText2!!.text.toString().length == 0)
                Toast.makeText(this, "You did not enter a email address", Toast.LENGTH_SHORT).show();

            if (editText3!!.text.toString().length == 0)
                Toast.makeText(this, "You did not enter a comapny name", Toast.LENGTH_SHORT).show();
            return;
        }else if {

            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