2024年1月24日发(作者:)
package ionalapp;import ;import ;import em;import ;import ;import ;import Bar;import patActivity;public class SettingActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_setting); Button medium_btn = findViewById(_btn); ActionBar actionBar = getSupportActionBar(); assert actionBar != null; // set the behaviour sa same as bottom back button playShowHomeEnabled(true); playUseLogoEnabled(true); playHomeAsUpEnabled(true); Intent intent = getIntent(); boolean default1 = leanExtra("default", false); if (default1) { // the default level is medium onClickLevel(medium_btn); } } // using the bardware "Back" button to cancel adding a new item @Override public void onBackPressed() { xt(this, "Nothing pressed", _SHORT).show(); Pressed(); } // if clicking a level, jump to home page public void onClickLevel(View view) { Button button = (Button) view; String level; level = t().toString(); Intent intent = new Intent(, ); ra("level", level); setResult(RESULT_OK, intent); startActivity(intent); }}GameActivity之后就是最复杂的GameActivity了
private int initialNum; private TextView mQuizNumView; private int index; private final Questions mQuestions = new Questions(); // mQuestion is object; Question is class private String[] mCorrectAnswer; private String[] mImage; // initialize 2-dimension string array private final String[][] mChoice = {{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"},{"a", "a", "a"}}; @Override protected void onCreate(Bundle savedInstanceState) { te(savedInstanceState); setContentView(ty_game); Bundle levelData = getIntent().getExtras(); if (levelData == null) { return; } // randomize the question's sequence int[] questionSequence = randomCommon(0,11,10); // select difficulty based on clicked button String level = ing("level", "Medium"); initialNum = ionSelect(level); // the current question number QuestionNum = initialNum + questionSequence[index] - 1; mQuizNumView = findViewById(m_textview); textViewCountDown = findViewById(_view_countedown); mQuizImage = findViewById(_image); // get string resource from XML // get string from xml: mchoices, mAnswers, mImages String[] mChoices_3items = getResources().getStringArray(e); mCorrectAnswer = getResources().getStringArray(ctAnswer); mImage = getResources().getStringArray(); // split mChoice into three choices by comma // the pass one choice array into mChoice for (int i = 0; i < 30; i ++) { mChoice[i] = mChoices_3items[i].split(","); } textColorDefaultCd = tColors(); updateQuestion(); Button submit = findViewById(_submit); // once this activity is called, start counting down timeLeftInMillis = COUNTDOWN_IN_MILLIES; // shake detecter // it will implement hearShake() SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); ShakeDetector shakeDetector = new ShakeDetector(this); (sensorManager); startCountDown(); // start count down once create this activity lickListener(v -> { // Quiz logic
lickListener(v -> { // Quiz logic if (orrect(mAnswer, QuestionNum,mCorrectAnswer)) { // call the method by object displayToastCorrectAnswer(); mScore++; } else { displayToastWrongAnswer(); } (500); int numQuestion = gth(mImage) / 3 - 1; // this number is 9 always // index % 10 is the index in every 10 question; recall that QuestionNum is 0-30 if (ast(numQuestion, index % 10)) { // if approach the last quetion (call the method by object) pauseCountDown(); // jump into Result Activity Intent intent_result = new Intent(, ); intent_ra("totalQuestions", gth(mImage) / 3); intent_ra("finalScore", mScore); startActivity(intent_result); index = 0; // initialize all into zero mQuizNum = 0; mScore = 0; } else { // if not the last question index + 1 index++; QuestionNum = initialNum + questionSequence[index] - 1; mQuizNum++; updateQuestion(); // if game reach at the last question, the app wont update display Log.i("TestRandom", f(QuestionNum)); // it is good to include in code review } }); // sensor programming // learn from: /training/gestures/detector ouchListener(new hListener() { GestureDetector gestureDetector = new GestureDetector(getApplicationContext(), new OnGestureListener() { @Override public void onLongPress(MotionEvent e) { pauseCountDown(); Intent intent_home = new Intent(, ); // Long press to go to home page startActivity(intent_home); Press(e); } }); @Override public boolean onTouch(View v, MotionEvent event) { hEvent(event); return false; } }); } // generate random and unique integer array, range from 1-10 (minus 1 later) public static int[] randomCommon(int min, int max, int n){ int[] result = new int[n]; int count = 0; while(count < n) { int num = (int) (() * (max - min)) + min; // generate random integer inside the range boolean flag = true;
boolean flag = true; for (int j = 0; j < n; j++) { // if the generated integer is not inside the list, append it into list if(num == result[j]){ flag = false; break; } } if(flag){ result[count] = num; count++; } } return result; } // implement shakeListener // android studio is not sensitive, you have to shake it very very very strong to see the result @Override public void hearShake() { xt(getApplicationContext(), "The answer is " + rectAnswer(mCorrectAnswer,QuestionNum), _SHORT).show(); // shake to show the answer } // clock pause function private void pauseCountDown() { (); } // startDown(method) private void startCountDown() { countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) { // the first parameter is the limitation of countdown; the second parameter is the time interval @Override public void onTick(long millisUntilFinished) { timeLeftInMillis = millisUntilFinished; // assign the boundary updateCountDownText(); } @Override public void onFinish() { // if time up, jump to other activity and stop the clock timeLeftInMillis = 0; updateCountDownText(); xt(, "Times up", _LONG).show(); // if times up jump into result page and while store the result Intent intent_result = new Intent(, ); intent_ra("totalQuestions", gth(mImage) / 3); intent_ra("finalScore", mScore); startActivity(intent_result); (500); } }.start(); //start countdown immediately once creating it. } // this method is used to update count down timer private void updateCountDownText() { int minutes = (int) (timeLeftInMillis / 1000) / 60; // this is minutes int seconds = (int) (timeLeftInMillis / 1000) % 60; // this is seconds String timeFormatted = (ault(), "%02d:%02d", minutes, seconds); t(timeFormatted); // update the text if (timeLeftInMillis < 10000) { // if the time is less than 10 second, text turns red tColor();
tColor(); } else { tColor(textColorDefaultCd); } } // 2 methods below to show if answer is correct or not private void displayToastCorrectAnswer() { xt(, "Perfect!", _SHORT).show(); } private void displayToastWrongAnswer() { xt(, "Oh nooo!", _SHORT).show(); } // method to update the display private void updateQuestion() { LinearLayout answer_layout = findViewById(_layout); answer_AllViews(); // before view the new question we need remove the old questions mAnswer = ""; t(mQuizNum + "/" + gth(mImage) / 3); // display the number of current quiz showRadioButtonAnswers(QuestionNum); // call the defined function to update the answers showMainImage(); // display chinese character ScrollView sv = findViewById(View); ScrollTo(0, 0); } // method to show the image private void showMainImage() { String img = ge(mImage,QuestionNum); // get the image names geResource(getResources().getIdentifier(img, "drawable", getPackageName())); // display the characters } // method to show radio button answers private void showRadioButtonAnswers(int qnum) { int choiceIndex; final LinearLayout answerLayout = findViewById(_layout); RadioGroup rg = new RadioGroup(this); entation(AL); Params lp = new Params( _PARENT, // HEIGHT _CONTENT // WIDTH ); outParams(lp); ding(0, 0, 0, 0); // to ensure the choices are in the center final RadioButton[] rb1 = new RadioButton[3]; // create a RadioButton array, 3 value button int[] choiceSequence = randomCommon(0,4,3); // randomize the question's sequence, generate a random choice sequence for (int i = 0; i <= 2; i++) { rb1[i] = new RadioButton(this); choiceIndex = choiceSequence[i] - 1; // generate random choice index, select random choice from one string array rb1[i].setText(ice(mChoice,qnum)[choiceIndex]); // set the three choices side radio button rb1[i].setTextColor(); rb1[i].setTextSize(25); rb1[i].setId(i); rb1[i].setWidth(900); w(rb1[i]); // add into Radio button view group } heckedChangeListener((group, id) -> { id = choiceSequence[id] - 1; // here is my very very smart move specify the id into random consequence mAnswer = ice(mChoice,QuestionNum)[id]; });


发布评论