Get Value of a Edit Text field

前端 未结 12 1100
既然无缘
既然无缘 2020-11-22 07:20

I am learning how to create UI elements. I have created a few EditText input fields. On the click of a Button I want to capture the content typed into that input field.

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 07:58

    Try this code

    final EditText editText = findViewById(R.id.name); // your edittext id in xml
    Button submit = findViewById(R.id.submit_button); // your button id in xml
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            String string = editText.getText().toString();
            Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
        }
    });
    

提交回复
热议问题