I started learning Android Studio and Java after c #, js, php. During the study, every time I wondered why it was so difficult to work with the elements created on the layout? What happens to the variables? What is the strength of this approach?

For example, in order to get the text contained in the EditText, you need to do the following operation:

EditText userField = (EditText) findViewById(R.id.userFiledEditText); String s = userField.getText(); 

It's horrible! And even if after Sharp you don’t pay attention to the absence of var and the fact that you can’t refer directly to the elements whose IDs are already assigned (like js), THAT what .. person cannot immediately work with the result of the function?!?! What ?! Or do I not know something?

 Toast.makeText(getApplicationContext(),R.id.userFiledEditText).getText(), Toast.LENGTH_SHORT).show(); 

Dear, who is able to correctly explain this fact to me? I believe that all this is done for the sake of something good and carries a meaning, but I want to understand.

PS: without holivar and kicks from gurus javistov.

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants pavlofff , Sergey Gornostaev , Suvitruf , Kromster , JuriySPb 6 December '16 at 9:09 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    So it is necessary. And the other will not, you can accept or leave. It is also not clear what you want to see in response: to remake, how you want, to be allowed to do differently, to be comforted by a great idea ... or why this stream of emotional confusion? This question is absolutely unformat on this resource, where they solve practical problems, and do not console migrants from other platforms. - pavlofff
  • String s = ((EditText) findViewById (R.id.userFiledEditText)). UserField.getText (); - Sergey
  • You can use ButterKnife. Then it will be possible to do binding on annotations, for example, so @BindView(R.id.userFiledEditText) EditText userField; - Victor Khovanskiy

1 answer 1

Behind all this emotional husk, I seem to understand what the problem is.

The markup is written in the declared XML language, it does not contain widget objects with which it would be possible to manipulate the code. For conversion to objects, the markup must first inflate (parsing the conversion from XML to objects)

The type identifier R.id.name is an integer of type int. When parsing the markup, a utility class R is created, which associates the names of the widgets with numeric values ​​(ID). Then, using these IDs, you can retrieve an object that is marked with this ID from an array of View objects that were created during parsing the markup.

In order to use the methods applicable to the View, there must be an object of this type, which the number is obviously not.

Using the findViewById () method, you get an object to which a specific identifier is attached. Then you work with an object for which you can call methods.

PS: remove all this "horror" from the question "how is it so" and other emotions, these exclamations are not appropriate here.