Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 6 - Android 2 - COMP2100/6442 Skip navigation COMP2100/6442 ANU College of Engineering & Computer Science Search query Search ANU web, staff & maps Search COMP2100/6442 Exams menu Search query Search ANU web, staff & maps Search Search COMP2100/6442 labs Overview Lab 1 - Install Lab 2 - Git / SSH Lab 3 - Trees Lab 4 - Parser Lab 5 - Android 1 Lab 6 - Android 2 Lab 7 - Persistent Data Lab 8 - PHP Lab 9 - Lab Test 1 Lab Test 2 related sites Wattle Lab 6 - Android 2 NOTE: Like last week’s lab, you must attend the actual (virtual) lab in order to be able to receive marks. If you are unable to make it, please let your tutor(s) know beforehand with a valid reason and they will arrange an alternate time for you to present. The main objective of this lab is to expand on the voting application from lab 5 to keep track of how many times a particular choice was voted for and display the results. Note that you will need to have finished at least Task 1 of Lab 5 in order to be able to complete this lab since we are adding onto the same project. Task 1 - Count [0.4 Marks] We will be using the same project we created in Lab 5 for this week’s lab. Add 3 integer fields to your MainActivity class, which will keep track of the number of times each button was selected. Recall from last week that we were able to modify the TextView with the help of findViewById(). We can do the same thing with each Button, e.g. for the second choice: Button button = findViewById(R.id.button2); Remember that if you are unsure about the id, it can be found at the top of the button’s attributes in activity_main.xml. Button inherits from TextView, so the setText() method can be used to update it’s display text. Your task is to modify the methods that are called when your buttons are pressed so that instead of changing the question text, they increment the relevant counter (i.e. one of the fields you added at the start of this task) and display the count in the Button text. It is okay if you rewrite the entire text each time. In order to receive marks for this task you will need to show: That pressing each button increments updates it’s text to reflect the number of times it has been pressed Task 2 - Intents [0.3 marks] Create a new activity called ResultActivity. Modify it’s layout (activity_result.xml) such that it contains a TextView (set it’s id to resultsText) and a Button containing the text “Back”. Also add a Button to MainActivity called “Results”. Remember to add constraints to all these new Views. Set up a method that is called when the Results button is pressed. To move between activities, we need to first create an Intent, which describes an action which we can later perform: Intent intent = new Intent(getApplicationContext(), ResultActivity.class); To perform this action, we simply call startActivity() with the Intent: startActivity(intent); Now run your application and click the Results button. You should be taken to the ResultActivity. You can press the “physical” back button on the emulator (not the one you created) to return back to your MainActivity. If you try and implement the Back button in the ResultActivity in the same way, you’ll notice an issue where if you have pressed some of the choices in the MainActivity, go to the ResultActivity and attempt to return to by pressing the Back button, your choices will be reset. This is because this is an entirely new instance of your MainActivity (You can think of this as pushing to a stack, where your original MainActivity is now buried under your ResultActivity and a new instance of MainActivity). Instead, call the finish() method when the Back button is pressed (and nothing else). Instead of creating a new MainActivity, this will “pop” the ResultActivity off the stack and return you to your original MainActivity (This is also what pressing the back button on your emulator does). In order to receive marks for this task you will need to show: That pressing the results button leads to your ResultActivity Your ResultActivity contains a Back button. Pressing the Back button in your ResultActivity returns you to the original instance of your Main Activity Task 3 - Results [0.4 marks] Now, let’s pass some data between the activities. In your MainActivity, before you call startActivity(), use intent.putExtra() to add your data: intent.putExtra("choice1Num", choice1); This can be thought of as adding to a Map (or Dictionary), the first argument is the key and the second argument is the value. Our value will be the variable containing the number of times the first choice has been pressed. Call this function again to add the count of the other two choices to the Intent, making sure this is all done before calling startActivity(). Now head to the ResultActivity. The onCreate() method is called when the Activity is instantiated. Add the following to the bottom of it: TextView resultsText = findViewById(R.id.resultsText); int choice1 = getIntent().getIntExtra("choice1Num", 0); resultsText.setText("" + choice1); The getIntExtra() function accesses the extra data we added to our Intent. The 0 after the key is the default value, just in case the key does not exist. Note for your assignment there are other functions such as getStringExtra() that may be useful if the data you added was a String rather than an integer. Finally, the last line updates the TextView with the data we extracted from the intent. Run your application again, press the first choice 3 times and click the results screen. If you’ve done everything correctly, you should see something similar to this: Your task is to finish the results screen so that it presents the number of votes for each choice in a sensible manner. Feel free to get creative with multiple TextViews, sizes etc. In order to receive marks for this task you will need to show: That pressing the Results button leads to the ResultActivity The ResultActivity displays the number of times each choice was chosen before the Results button was pressed The information is presented in an easy to understand way (It should be obvious which number corresponds to which choice) Useful Resources The content covered in both this lab and the previous one will be useful as a base for your group assignment. The next labs will cover persistent data and aspects of server-based features which will be useful for some of the more difficult features you may choose to implement for your assignment. These resources (all from the developer reference - make sure you select java as the language in the code snippets) may be a good starting point for implementing some of the other default features: General RecyclerView Requesting Permissions Services Scheduling Calendar Push Notifications Location Google Maps Integration Location Reference Updated:  / Responsible Officer:  / Page Contact:   Contact ANU Copyright Disclaimer Privacy Freedom of Information +61 2 6125 5111 The Australian National University, Canberra CRICOS Provider : 00120C ABN : 52 234 063 906 You appear to be using Internet Explorer 7, or have compatibility view turned on. Your browser is not supported by ANU web styles. » Learn how to fix this » Ignore this warning in future