"The authoritative Guide to Android Programming"-Reading notes (v)
-Challenge Practice
In order to strengthen learning, in order to build confidence. Start with the following challenges:
Challenge Exercise one: adding listeners to TextView
The book gives hints, Textview,button are all the sub-classes of view, can be set Onclicklistener () directly add the following code:
Mquestiontextview.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
Mcurrentindex = (mcurrentindex+1)% Mquestionbank.length;
Updatequestion ();
}
});
This modification modifies a file and binds an event to TextView directly at the logical level.
Add the following code:
http://git.oschina.net/canglin/GeoQuiz/commit/6b35ec258c780c9a9d4016c9c3a29e568f21a489
Challenge Exercise II: Add a Back button
- First, we're going to add controls to the Layout/activity_quiz.xml.
- Then we add the text on the button in the Values/strings.xml.
- Defining Variables for Mprevbutton
- Defining events for Prevbutton
When the controls are added, the buttons are arranged in order, and in order for the 2 button to be on one line, I use a linearlayout whose Orientation property is horizontal.
When I define the text of the button, I still use the @string/prev_button, and then I add the text inside the Strings.xml.
When the Prev button arrives at the first of the problem, we stop the fallback. The code is as follows:
Mprevbutton.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
if (Mcurrentindex >0)
{
mcurrentindex--;
}
Updatequestion ();
}
});
I've modified 3 files for this challenge.
The code is as follows:
http://git.oschina.net/canglin/GeoQuiz/commit/a7868341ae5efff75a024f5e7e6f5197875d0517
Challenge Exercise III: From the button to the icon button
Requirement: Replace the normal button component on the user interface with the ImageButton component.
The ImageButton component inherits the ImageView. The button component inherits TextView. Their inheritance relationship is as follows:
So they can be replaced directly, but ImageButton is not the Text property, so this property must be deleted.
so the code should look like this. Of course , 2 variables Mprevbutton,mnextbutton Remember to change to ImageButton type, there are logic code inside to get object instance also to change to ImageButton.
2 files modified in this modification
The code is as follows:
http://git.oschina.net/canglin/GeoQuiz/commit/3ceadc8f6089801f187a697dfb03185c31c72612
This challenge has been rewarded with the following:
- familiar with adding Onclicklistener () to TextView, Button, ImageButton
- LinearLayout is used for layout.
- The new control ImageButton, and the relationship to the button.
"The authoritative Guide to Android Programming"-Reading Notes (v)-Challenge practice