11
The visual components of Android as EditText
, Button
and others, possess the Listeners to handle events triggered by actions performed by users.
Therefore, in the method corresponding to the event, it is always necessary to pass a View
as parameter. See a small example:
Button btn = (Button) findViewById(R.id.botaoMsg);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View view)
{
EditText edtMsg = (EditText) findViewById(R.id.edtMsg);
String msg = edtMsg.getText().toString();
if (!msg.trim().isEmpty()) Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
else Toast.makeText(getApplicationContext(), "Digite uma mensagem!", Toast.LENGTH_SHORT).show();
}
});
Note that in the method onClick()
the variable view
and in the method findViewById()
is looking for a EditText
that might be a View.
This is where my doubts regarding class arise View
and View on Android.
Doubts
- I have always seen a View as the representation of the entire graphical interface of an application, however in this case the View does not seem to assume this role, so I would like to know what is a
View
on Android? - What is the purpose of the class
View
? - What is the importance of this class in relation to visual components android?
From what I see, it seems to be just a screen control. In the GWT has the
Widget
, in Totalcross has theControl
and the AWT has theComponent
. A generic deal that can be plotted on the screen– Jefferson Quesado
AWT Component
– Jefferson Quesado
Related: What is the difference between Activity and View on Android?
– viana
The parameter
view
in this case is theButton
. You could cast him for the typeButton
and work with it (for example, changing the color of the button when it was clicked).– Piovezan