Now that the buttons have resource IDs, you can access them in QuizActivity. The first step is to add two member variables. Type the following code into QuizActivity.java. (Do not use code completion; type it in yourself.) After you save the file, it will report two errors.
Listing 1.7 Adding member variables (QuizActivity.java)
public class QuizActivity extends AppCompatActivity {private Button mTrueButton;private Button mFalseButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
...
}
You will fix the errors in just a second. First, notice the m prefix on the two member (instance) variable names. This prefix is an Android naming convention that we will follow throughout this book. Now mouse over the red error indicators. They report the same problem: Cannot resolve symbol 'Button'.
These errors are telling you that you need to import the android.widget.Button class into QuizActivity.java. You could type the following import statement at the top of the file: import android.widget.Button;
Or you can do it the easy way and let Android Studio do it for you. Just press Option+Return (or Alt+Enter) to let the IntelliJ magic under the hood amaze you. The new import statement now appears with the others at the top of the file. This shortcut is generally useful when something is not correct with your code. Try it often! This should get rid of the errors. (If you still have errors, check for typos in your code and XML.) Now you can wire up your button widgets. This is a two-step process: get references to the inflated View objects; set listeners on those objects to respond to user actions
Getting references to widgets
In an activity, you can get a reference to an inflated widget by calling the following Activity method: public View findViewById(int id) This method accepts a resource ID of a widget and returns a View object. In QuizActivity.java, use the resource IDs of your buttons to retrieve the inflated objects and assign them
to your member variables. Note that you must cast the returned View to Button before assigning it.
Listing 1.8 Getting references to widgets (QuizActivity.java)
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button) findViewById(R.id.true_button);
mFalseButton = (Button)findViewById(R.id.false_button);
}
...
}
Listing 1.7 Adding member variables (QuizActivity.java)
public class QuizActivity extends AppCompatActivity {private Button mTrueButton;private Button mFalseButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
}
...
}
You will fix the errors in just a second. First, notice the m prefix on the two member (instance) variable names. This prefix is an Android naming convention that we will follow throughout this book. Now mouse over the red error indicators. They report the same problem: Cannot resolve symbol 'Button'.
These errors are telling you that you need to import the android.widget.Button class into QuizActivity.java. You could type the following import statement at the top of the file: import android.widget.Button;
Or you can do it the easy way and let Android Studio do it for you. Just press Option+Return (or Alt+Enter) to let the IntelliJ magic under the hood amaze you. The new import statement now appears with the others at the top of the file. This shortcut is generally useful when something is not correct with your code. Try it often! This should get rid of the errors. (If you still have errors, check for typos in your code and XML.) Now you can wire up your button widgets. This is a two-step process: get references to the inflated View objects; set listeners on those objects to respond to user actions
Getting references to widgets
In an activity, you can get a reference to an inflated widget by calling the following Activity method: public View findViewById(int id) This method accepts a resource ID of a widget and returns a View object. In QuizActivity.java, use the resource IDs of your buttons to retrieve the inflated objects and assign them
to your member variables. Note that you must cast the returned View to Button before assigning it.
Listing 1.8 Getting references to widgets (QuizActivity.java)
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button) findViewById(R.id.true_button);
mFalseButton = (Button)findViewById(R.id.false_button);
}
...
}






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