Lesson 10. Optimizing the implementation of handlers.
In this tutorial, we will:
- learn how to use a single handler for multiple Viewelements
- teach an Activity to act as a handler
Create project:
Project name: P0101_Listener
Build Target: Android 2.3.3
Application name: Listener
Package name: ru.startandroid.develop.listener
Create Activity: MainActivity
We will work with the same Viewsas in the previous lesson. Code formain.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
android:id="@+id/linearLayout1"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_margin="30dp">
android:layout_width="wrap_content"
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/tvOut"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="50dp">
|
One handler for two buttons
So, we have a TextView with text and two buttons. As in the previous lesson, we will make sure that the content of the TextView changes when you click the button. By pressing the OK button, we will display the text: "OK button is pressed", by pressing Cancel – "Cancel button is pressed". But for now, we will do this with a single handler that will handle clicks for both buttons.
Let me remind you of the event handling mechanism on the example of a button click. The button itself can't handle clicks. It needs a listener, which is assigned using the setOnClickListener methodsetOnClickListener. When the button is clicked, the handler responds and executes the code from the onClick method.
Accordingly, to implement it, you need to perform the following steps::
- create a handler
- fill in the onClick method
- assign a handler to the button
In our case, we will assign one handler to both buttons, and inside the handler we will need to determine which button was clicked.
Preparing objects and creating a handler:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public class MainActivity extends Activity {
TextView tvOut;
Button btnOk;
Button btnCancel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// найдем View-elements
tvOut = (TextView) findViewById(R.id.tvOut);
btnOk = (Button) findViewById(R.id.btnOk);
btnCancel = (Button) findViewById(R.id.btnCancel);
// создание обработчика
OnClickListener oclBtn = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
}
}
|
Let's fill in the onClick method. The input is an object of the View class, which is exactly what we need. This is the Viewthat the click occurred on, and that triggered the handler. I.e. in our case, it will be either the OK or Cancel button. All we have to do is find out the IDof this View and compare it with our R.id. btnOk and R.id. btnCancel to determine which button it is. To get the ID of a View, use getId. To iterate through the results, we use in the java switch.
Implementation of the onClick method:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public void onClick(View v) {
//by id , we determine the button that called this
switch (v) handler.getId()) {
case R. id. btnOk:
/ / кнопка OK button
TVout.setText("Нажата кнопка OK button pressed);
break;
case R. id. btnCancel:
/ / кнопкаCancel button
TVout. setText("Нажата кнопкаCancel " button is pressed);
break;
}
}
|
If you run the app now and check it, nothing will happen. We created a task handler, but we didn't assign it to the buttons. Assign the same handler to both buttons:
1
2
|
btnOk.setOnClickListener(oclBtn);
btnCancel.setOnClickListener(oclBtn);
|
Now we can run and check, everything should work.
As you can see, a single handler can be assigned to any number of buttons instead of two. And not just for buttons. The other Viewelements also have various events that need handlers. We will continue to work with them in the future. But now it is important to understand the scheme of how events are processed.
The difference between the implementation method in this lesson and the previous lesson is that now we have created one handler object for both buttons, and in the last lesson-two objects, one for each button. There is a rule – the fewer objects you create, the better, because memory is allocated for each object, and this is a fairly limited resource, especially for phones. Therefore, creating a single handler for multiple Views is more correct from the point of view of optimization. In addition, the code becomes smaller and easier to read.
There is another way to create a handler that does not require creating objects at all. The already created Activity object will be used Activity
Activity, as a handler
The button assigns a handler to itself using setOnClickListener (View.OnClickListener l). with View.OnClickListener. Why wouldn't the Activity class be such an object? We simply specify that the Activityclass implements the View. OnClickListener interface and fill in the onCreate method.
Creating a new project for this purpose:
Do'stlaringiz bilan baham: