Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 5 - Android 1 - 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 5 - Android 1 The main objective of this lab is to create a simple voting application that displays which option the user voted for. Note that a lot of the content is covered in the Week 5 Lecture, which may be useful to (re-)watch if you are unsure about whether you are doing something correctly or not. Task 0 - Project Setup Create a new Android Studio project, with the EmptyActivity template. Ensure that the language is set to Java and your minimum API is at least API 21. You may have to wait a while for Gradle to configure your project. Ensure that you are connected to the internet while this is occurring. It should be done when you see “Gradle sync finished in …” in the bottom left of the window. Click the green play button to run your project. You will need to create a new virtual device if you haven’t installed one yet. If so, head to Tools > AVD Manager > Create Virtual Device, select a device you would like to emulate (ensure it is in the phone tab), click Next and download a system image with API level 29. Once this is done click Next and Finish. If you encounter an error while installing the system image, see the bottom of the page for troubleshooting. You should now be able to select your virtual device (if it is not already selected) and run it. Once the emulator has finished loading you should see a screen that looks like this: Task 1 - GUI [0.5 marks] If you go back to your project, there should be a file open called activity_main.xml (if not, you can find it under app > res > layout). This will show a preview of what the activity will look like on a device. Click on the Hello World! text box (known more formally as a TextView). You will be able to see its attributes in the right panel. At the top, set its id to text and change the text Common Attribute to any multiple-choice question of your choice. In the top left panel you should see the palette of available views. Drag the Button view into the preview, placing it below the Hello World! TextView. Run the application. You’ll notice that the button doesn’t appear where you dragged it to. This is because it is unconstrained. If you click on it, you’ll notice there are no jagged lines coming out of it compared to when you clicked on the TextView. These lines represent constraints, which specify how the view should be aligned in relation to the other views in the application. This is useful when our application is dynamic. Imagine an example where we have an image with a caption below it. If we specify an exact position for our caption it may be covered if the image is changed to one that is a larger size. However, we can specify a constraint that it should always be right below (or something more specific such as 10 pixels below) the image to ensure it is never overlapped. We can also constraint it in relation to the screen itself. Click the Button and the left and right blue plus buttons in the Constraint Widget under Layout. Change these values to 0. This will constrain the button to both the left and right edges of the screen, centering it. We can also specify constraints visually. Drag the top white dot above the button and connect it with the white dot below the TextView (will only appear when you have started dragging). If you have done it correctly, it should snap to the TextView. Now drag the entire button down a little bit. You’ll notice a line with a number between it appearing. In the above image the button has been constrained to always be spaced 60 dp (density-independent pixels) below the TextView. You can see this by dragging the TextView up and down. Notice how the button maintains the same distance from the TextView. You can also experiment with constraining to either the top or bottom edge of the screen (you will need to make sure you connect it with the exact edge of the screen). Your task is to create 2 additional buttons and arrange the Activity such that it presents your question and 3 possible answers in a sensible way. Feel free to experiment with the constraints and attributes such as textAppearance. Your Views should always have at least one horizontal and one vertical constraint if you don’t want them to “float” around. In order to receive marks for this task you will need to show: The working MainActivity running on your emulator containing: A TextView with a question of your choice 3 buttons labelled with valid responses to this question That each of these elements is sensibly positioned (no overlaps or extreme misalignments) Task 2 - Basic Actions [0.5 marks] Let’s make pressing the buttons actually perform an action. Click on the button corresponding to your first choice and change its onClick attribute (under Common Attributes) to choice1Pressed. This specifies the method that is called when the button is pressed. Head back to MainActivity.java and add this method to the MainActivity class: public void choice1Pressed(View view) { } We will now update the TextView when this method is called (i.e. the button has been pressed). First, we need to find the TextView so we can modify it. Recall in Task 1 that we set the TextView’s id to text. This means we can find it using the identifier R.id.text. TextView textView = findViewById(R.id.text); Finally, use the setText() method to update the displayed string. textView.setText("Your string here"); Now try running the application and clicking the button! Your task is to make the other two buttons also update the TextView. In order to receive marks for this task you will need to show: That the TextView updates to reflect the option chosen for each of the three buttons when they are pressed. If you have some free time after you are done, feel free to experiment with Intents (shown in the first Android lecture) to make pressing each button lead to either a different activity or a single activity that displays different text depending on which button was pressed. Next week we’ll improve upon this application to make it keep track of the number of times each option was chosen and display the results, which will hopefully make it a lot more useful. Marking Once you have finished both tasks (or just the first one if you have run out of time), let your tutor(s) know and one of them will initiate a private call with you. You will need to share your screen in order to show your code and emulator to demonstrate that you have completed the tasks correctly. Troubleshooting - Virtual Device If you receive an error about Intel HAXM Manager (or similar) not being installed properly follow the instructions pertaining to your operating system to determine your CPU manufacturer: Windows: Press Windows + X and click System or search for System Information in the search bar (Windows 7 and 10) . Windows 8 users can press Windows + R, type msinfo32 and click OK. Determine whether it is Intel or AMD by looking at the Processor name. Linux: Open a new terminal window and run the lscpu command. Check the Vendor ID to determine whether it is GenuineIntel (meaning your manufacturer is Intel) or AuthenticAMD (your manufacturer is AMD). Mac OS (OSX): Your manufacturer will (almost certainly) be Intel. Once you have determined your manufacturer follow the appropriate steps: Intel: Head to Tools > SDK Manager > SDK Tools, check Intel x86 Emulator Accelerator (HAXM Installer) and click OK. Follow the installer. If you are on Windows and are still receiving errors, there may be an installer in C:\users\%Your_Username %\AppData\Local\Android\sdk\extras\intel\Hardware_Accelerated_Execution_Manager\ that you can run instead. AMD: Head to Tools > SDK Manager > SDK Tools, check Android Emulator Hypervisor Driver for AMD Processors (installer) and click OK. Follow the installer. Once you have finished either the Intel or AMD Steps: Make sure that Android Emulator (also in the SDK Tools Tab) is fully checked (not just a dash) and click OK. If you are still experiencing issues, please let your tutor know and they will attempt to assist you with troubleshooting. Please be patient as you may have an issue they have not experienced before and you will need to work together to solve it. 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