CS计算机代考程序代写 android 15/06/2021 Week 10: Workshop Quiz: Attempt review

15/06/2021 Week 10: Workshop Quiz: Attempt review
FIT2081 Mobile application development – S1 2021
Dashboard / My units / FIT2081_S1_2021 / Assessments / Week 10: Workshop Quiz
Started on State Completed on Time taken Grade
Print friendly format
Question 1
Monday, 10 May 2021, 8:40 PM Finished
Monday, 10 May 2021, 11:15 PM 2 hours 34 mins
8.75 out of 10.00 (88%)
Question 1
Complete
Mark 8.75 out of 10.00
Q1 – If you have an activity that uses constraint layout with id=“layout_21_s1”, write a piece of code that shows in a toast the distance in pixel between a click event that occurs on the activity and the left edge of the screen. (20m)
packagecom.example.workshopW10;
importandroidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
import android.view.MotionEvent; import android.view.View;
import android.widget.Toast;
publicclassMainActivityextendsAppCompatActivity { ConstraintLayout constraintLayout;
@Override protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
https://lms.monash.edu/mod/quiz/review.php?attempt=19830101&cmid=8331156 1/5

15/06/2021 Week 10: Workshop Quiz: Attempt review
Toast.LENGTH_SHORT);
}
View constraintLayout = findViewById(R.id.layout_21_s1); view.setOnTouchListener(new View.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv, MotionEventevent) {
int action = event.getActionMasked();
float xCoord = event.getX();
float yCoord = event.getY();
String message =String.format(“(X,Y) coordinates = (%.2f, %.2f)”, xCoord, yCoord);
switch (action){
case (MotionEvent.ACTION_UP ):
Snackbar mySnackBar=Snackbar.make(frameLayout,message, Snackbar.LENGTH_SHORT); mySnackBar.show();
break;
}
return true; }
});
constraintLayout.setOnTouchListener(newView.OnTouchListener() { @Override
publicbooleanonTouch(Viewv, MotionEventevent) {
int action = event.getActionMasked();
float xCoord = event.getRawX();
String message =String.format(“Distance: %.2f”, Math.abs(xCoord)); Toast myToast= Toast.makeText (getBaseContext(), message,
return true; }
}); }
switch (action){
case (MotionEvent.ACTION_UP ):
myToast.show();
break;
case (MotionEvent.ACTION_DOWN):
myToast.show();
break;
case (MotionEvent.ACTION_MOVE ):
myToast.show();
break; }
https://lms.monash.edu/mod/quiz/review.php?attempt=19830101&cmid=8331156 2/5

15/06/2021 Week 10: Workshop Quiz: Attempt review
Comment:
(Q1=90% Q2=100% Q3=71% )
Question 2
Q2 – Briefly explain the role of the getActionMasked’s output. (5m) int i=motionEvent.getActionMasked;
The getActionMasked() method returns an integer number (constant) that represents the type of the current event. The action is then compared with pre-defined constant values to determine the type of the current event.
In int i = motionEvent.getActionMasked(), getActionMasked() will return just an event (up, down, move) information without showing any other event information i.e all other information will be masked out.
The role of getActionMasked() is that it will be used to check whether the click event is UP, DOWN, MOVE and based on that output.
Question 2 Complete Not graded
Question 3 Complete Not graded
Question 3
Q3- The following piece of code is designed to show a toast with each Action Up event. But, it shows nothing. Find and explain the problem, then fix it. (7m)
Action UP event that can not occur without having any prior DOWN and MOVE event, so UP can only come after DOWN or MOVE event. To solve this problem, I need to add the Down and Move section to this code.
view.setOnTouchListener(new View.OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) { int action = event.getActionMasked(); switch(action){
case(MotionEvent.ACTION_DOWN) :
Toast.makeText(self,”Action DOWN”,Toast.LENGTH_SHORT).show(); return true;
case(MotionEvent.ACTION_MOVE) :
Toast.makeText(self,”Action MOVE”,Toast.LENGTH_SHORT).show();
https://lms.monash.edu/mod/quiz/review.php?attempt=19830101&cmid=8331156 3/5

15/06/2021 Week 10: Workshop Quiz: Attempt review
return true; case(MotionEvent.ACTION_UP) :
Toast.makeText(self,”Action UP”,Toast.LENGTH_SHORT).show();
return true; default :
return false; }
} });
Question 4
Question 4 Not answered Not graded
Question 5 Not answered Not graded
Question 5
https://lms.monash.edu/mod/quiz/review.php?attempt=19830101&cmid=8331156 4/5

15/06/2021 Week 10: Workshop Quiz: Attempt review
◄ Week 9: Workshop Quiz
Jump to…
Week 11: Workshop Quiz ►
https://lms.monash.edu/mod/quiz/review.php?attempt=19830101&cmid=8331156 5/5