From Layout XML to View Objects
How do XMLelements in activity_quiz.xml become View objects? The answer starts in the QuizActivity class. When you created the GeoQuiz project, a subclass of Activity named QuizActivity was created for you. The class file for QuizActivity is in the app/java directory of your project. The java directory is where the Java code for your project lives. In the Project tool window, reveal the contents of the app/java directory and then the contents of the com.bignerdranch.android.geoquiz package. Open the QuizActivity.java file and take a look at its contents (Listing 1.4).
Listing 1.4 Default class file for QuizActivity (QuizActivity.java)
package com.bignerdranch.android.geoquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class QuizActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
(Wondering what AppCompatActivity is? It is a subclass of Android’s Activity class that provides compatibility support for older versions of Android. You will learn much more about AppCompatActivity in Chapter 13.)
If you are not seeing all of the import statements, click the symbol to the left of the first import statement to reveal the others.
This file has three Activity methods: onCreate(Bundle), onCreateOptionsMenu(Menu), and
onOptionsItemSelected(MenuItem). Ignore onCreateOptionsMenu(Menu) and onOptionsItemSelected(MenuItem) for now. You will return to menus in detail in Chapter 13.
The onCreate(Bundle) method is called when an instance of the activity subclass is created. When an activity is created, it needs a user interface to manage. To get the activity its user interface, you call the following Activity method: public void setContentView(int layoutResID) This method inflates a layout and puts it on screen. When a layout is inflated, each widget in the layout file is instantiated as defined by its attributes. You specify which layout to inflate by passing in the
layout’s resource ID.
How do XMLelements in activity_quiz.xml become View objects? The answer starts in the QuizActivity class. When you created the GeoQuiz project, a subclass of Activity named QuizActivity was created for you. The class file for QuizActivity is in the app/java directory of your project. The java directory is where the Java code for your project lives. In the Project tool window, reveal the contents of the app/java directory and then the contents of the com.bignerdranch.android.geoquiz package. Open the QuizActivity.java file and take a look at its contents (Listing 1.4).
Listing 1.4 Default class file for QuizActivity (QuizActivity.java)
package com.bignerdranch.android.geoquiz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class QuizActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.quiz, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
(Wondering what AppCompatActivity is? It is a subclass of Android’s Activity class that provides compatibility support for older versions of Android. You will learn much more about AppCompatActivity in Chapter 13.)
If you are not seeing all of the import statements, click the symbol to the left of the first import statement to reveal the others.
This file has three Activity methods: onCreate(Bundle), onCreateOptionsMenu(Menu), and
onOptionsItemSelected(MenuItem). Ignore onCreateOptionsMenu(Menu) and onOptionsItemSelected(MenuItem) for now. You will return to menus in detail in Chapter 13.
The onCreate(Bundle) method is called when an instance of the activity subclass is created. When an activity is created, it needs a user interface to manage. To get the activity its user interface, you call the following Activity method: public void setContentView(int layoutResID) This method inflates a layout and puts it on screen. When a layout is inflated, each widget in the layout file is instantiated as defined by its attributes. You specify which layout to inflate by passing in the
layout’s resource ID.






0 nhận xét:
Đăng nhận xét