Most recently, I wrote a
post in which I expressed my desire to move from development for Windows Phone 7 to Android programming. Unfortunately, some external circumstances have slowed down this process and the study is not going as fast as we would like. Nevertheless, the desire has not diminished, and I will soon be able to devote more time to Android.
And today I want to talk about such an ordinary thing as the processing of a click of a button. The article is intended for beginners and will talk about obvious things, but it cost me one night.
Small retreat
When I was eager to learn programming under Android, through a search query, Google tried to get a list of Russian-language sites where there were manuals for Android. In principle, the documentation in English does not frighten me, but I wanted to quickly get into the specifics of programming. But the abundance of new terms and concepts at first could hinder such an understanding by reading the documentation in the original. I honestly discovered about twenty different links and the result disappointed me. Practically everywhere, only two articles were offered as a guide: installing the necessary software and translating the Hello, World article from the official developer.android.com website. It seems that all together skopipasti from one source, added several articles from Habr and all. I would be glad if someone tells me a good resource for very beginners - ideally, articles should be written for people who were not familiar with programming at all before they became acquainted with Android.
Go to the button. After writing the traditional Hello, World, which was displayed in TextView immediately after downloading the application, I wanted to try something more functional. Having dealt with the basic principles of placing elements in the xml-file, I decided to rewrite Hello, World in a more familiar manner. I added an EditText text box and a Button button to the form, and began to search the documentation for a way to handle clicks. The documentation (as well as articles on Habré) used such a long construction:
// Create an anonymous implementation of OnClickListener private OnClickListener mCorkyListener = new OnClickListener() { public void onClick(View v) { // do something when the button is clicked } }; protected void onCreate(Bundle savedValues) { ... // Capture our button from layout Button button = (Button)findViewById(R.id.corky); // Register the onClick listener with the implementation above button.setOnClickListener(mCorkyListener); ... }
Whether the late hour or general tiredness affected, but my head refused to memorize this construction. Perhaps for java-developers this is a standard procedure, not causing difficulties, but not for me.
And then, rereading the examples on Habré, I suddenly came across one comment, which said that under Android 2.x you can use a different design. Thank you, kind man! His remark saved me. Everything can be done much easier.
Open the main.xml file and look for the
<Button> tag . Add one more line to it:
<Button android:layout_height="wrap_content" android:id="@+id/butHello" android:layout_width="wrap_content" android:text="" android:onClick="butHello_Click"></Button>
')
Thus, we defined an
onClick event for the button (click) and now we have to write an event handler. Open the HelloWorld.java file and write immediately after the onCreate event:
public void butHello_Click(View v){ edtext.setText(", "); }
Please note that the name of the
butHello_Click method should match the string that we specified in the
android: onClick attribute for the button, and also add brackets with the text
View v . In curly brackets we write the code that should be executed when the button is clicked. Since we need to display the text in the text field, we simply assign the desired text through the
setText method.
The full code for
HelloWorld.java will look like this.
package com.example.helloworld; import android.app.Activity; import android.os.Bundle; import android.widget.EditText; import android.view.View; public class HelloWorld extends Activity { private EditText edtext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); edtext = (EditText)findViewById(R.id.editText1); } public void butHello_Click(View v){ edtext.setText(", "); } }
After such a discovery, a mountain fell off my shoulders. First, this code looks simpler and clearer to me. Secondly, it is almost identical to the code for Windows Phone 7. Compare the XAML code for the button.
<Button Content="Button" Height="71" Width="160"
Name="butHello"
Click="butHello_Click"/>

A similar approach in the future will help simplify porting programs to different platforms. Unfortunately, for some reason, this method of processing a click of a button is rarely seen in the examples. I don’t know what caused this, but my opinion is that if Google has added this method for new versions of Android, then they need to use it more actively.
In conclusion, I will give my page where I post my first experiments in the development area for Android -
developer.alexanderklimov.ru/android . And I remind my request - tell me good Russian-language resources for quite beginners in the
basics of developing for Android.