2016-09-30 98 views
1

我开发了一个应用程序,它有两个按钮(左和右)和一个文本视图,可以在屏幕上弹出。每个按钮都有相应的单词。
用户必须在显示时尽快单击与TextView单词对应的按钮。我想通过点击按钮来计算它的反应时间。如何计算反应时间?

以下是我的代码。

public class Place_to_go_1 extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_place_to_go_1); 

     placeone = Global_variables.getFirst_choice_label(); 
     placetwo = Global_variables.getSecond_choice_label(); 
     p_one = (TextView)findViewById(R.id.p_one); 
     p_two = (TextView)findViewById(R.id.p_two); 

     btnleft = (ImageButton)findViewById(R.id.btnleft); 
     btnright = (ImageButton)findViewById(R.id.btnright); 

     next = (ImageButton)findViewById(R.id.Next); 
     lblmaintext = (TextView)findViewById(R.id.lblmaintext); 
     lblprompt = (TextView)findViewById(R.id.lblprompt); 
     lblreact = (TextView)findViewById(R.id.lblreact); 
     imgmain = (ImageView)findViewById(R.id.imgmain); 
     //prac = (ImageView) findViewById(R.id.prac); 

     Intent intent = getIntent(); 
     final String randomId = intent.getStringExtra("Info_id"); 

     //============ validate image if not empty 
     setImage_onLaunch(); 

     //==== populate left and right choices=== 
     populate_headers(placeone, placetwo); 
     //==== populate attributes===== 
     populate_attributes(); 

     //============== instruction ====== 
     setInstruction(); 

     //=============media 
     wrong_press = MediaPlayer.create(this, R.raw.wrong_press); 
     react_fast = MediaPlayer.create(this, R.raw.react_faster); 

     //=== left button click trigger 
     btnleft.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String position = "H"; 
        if (tanan[counter].equals(p_one.getText().toString())) { 

         lblprompt.setVisibility(View.INVISIBLE); 

         HashMap<String,String> queryValues = new HashMap<String, String>(); 
         queryValues.put("Info_id",randomId); 
         queryValues.put("Choice",p_one.getText().toString()); 
         queryValues.put("Reaction_time",String.valueOf(elapsedTime)); 
         queryValues.put("Position",position); 
         queryValues.put("Main",main); 
         queryValues.put("Error",error); 
         mydb.insertTest(queryValues); 


         counter++; 
         if (counter < tanan.length) { 
          btnleft.setEnabled(false); 
          btnright.setEnabled(false); 
          timeStamp = System.currentTimeMillis(); 
          //Toast.makeText(Place_to_go_1.this, ""+timeStamp, Toast.LENGTH_SHORT).show(); 
          getreactionTime(p_one.getText().toString(), String.valueOf((((timeStamp)/1000.0) - ((timeRun)/1000.0)))); 
          setIntervalTime(); 

         } else { 
          //======end sa data 

          postEnd(); 
         } 
        } else { 
         // Toast.makeText(Place_to_go_1.this, "Wrong pressed", Toast.LENGTH_SHORT).show(); 
         //wrong_press.start(); 
         wrong_click_audio(); 
         error = "1"; 
         lblprompt.setVisibility(View.VISIBLE); 
        } 


      } 
     }); 
     //==== right button click trigger 
     btnright.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       String position = "A"; 

        if (tanan[counter].equals(p_two.getText().toString())) { 

         lblprompt.setVisibility(View.INVISIBLE); 



         HashMap<String,String> queryValues = new HashMap<String, String>(); 
         queryValues.put("Info_id",randomId); 
         queryValues.put("Choice",p_two.getText().toString()); 
         queryValues.put("Reaction_time", String.valueOf(elapsedTime)); 
         queryValues.put("Position",position); 
         queryValues.put("Main",main); 
         queryValues.put("Error",error); 
         mydb.insertTest(queryValues); 



         counter++; 
         if (counter < tanan.length) { 
          btnleft.setEnabled(false); 
          btnright.setEnabled(false); 
          timeStamp = System.currentTimeMillis(); 
          //Toast.makeText(Place_to_go_1.this, ""+timeStamp, Toast.LENGTH_SHORT).show(); 
          getreactionTime(p_two.getText().toString(), String.valueOf((((timeStamp)/1000.0) - ((timeRun)/1000.0)))); 
          setIntervalTime(); 
         } else { 


          //======end sa data 
          postEnd(); 
         } 
        } else { 
         // Toast.makeText(Place_to_go_1.this, "Wrong pressed", Toast.LENGTH_SHORT).show(); 
         // wrong_press.start(); 
         wrong_click_audio(); 
         error = "1"; 

         lblprompt.setVisibility(View.VISIBLE); 
        } 
       } 



     }); 


     // ==== next button for the next activity (Place to go 2) 
     next.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent = getIntent(); 
       String randomId = intent.getStringExtra("Info_id"); 


       //============= launch activity 2 for place to go 
        if (instruct == true) { 
         next.setVisibility(View.INVISIBLE); 
         // prac.setVisibility(View.VISIBLE); 
         CountDownTimer(); 

          } else { 
           //Toast.makeText(getApplication(),"Saved Successfully.",Toast.LENGTH_SHORT).show(); 
           Intent i = new Intent(getApplicationContext(), Place_to_go_2.class); 
           i.putExtra("Info_id", randomId); 
           startActivity(i); 


        } 

      } 
     }); 


    } 



public void interval(){ 
     if(counter < tanan.length){ 
     lblmaintext.setVisibility(View.VISIBLE); 
     timeRun = System.currentTimeMillis(); 
     btnleft.setEnabled(true); 
     btnright.setEnabled(true); 
     lblmaintext.setText(tanan[counter]); 
     setImage(); 
     imgmain.setVisibility(View.VISIBLE); 

     react = true; 
     reacFaster(); 
     }else{ 
     //======end sa data 
     Toast.makeText(Place_to_go_1.this, "End data", Toast.LENGTH_SHORT).show(); 
     lblmaintext.setVisibility(View.VISIBLE); 
     lblmaintext.setText("Ok for now"); 
     } 


     } 

public void setIntervalTime(){ 
     react = false; 
     lblreact.setVisibility(View.INVISIBLE); 
     reactFaster_timer.cancel(); 
     lblmaintext.setVisibility(View.INVISIBLE); 
     lblreact.setVisibility(View.INVISIBLE); 
     imgmain.setVisibility(View.INVISIBLE); 

     timer = new CountDownTimer(Global_variables.interval_time_before_choices_will_show,Global_variables.interval_time_before_choices_will_show) { 
@Override 
public void onTick(long millisUntilFinished) { 

     } 

@Override 
public void onFinish() { 
     interval(); 
     } 
     }.start(); 
     } 
     int counter_countdown = 0; 
     int drawwable_amber = R.drawable.amber; 
     String arr[] = {"Ready...","Set...","Start."}; 
public void CountDownTimer(){ 

     btnleft.setVisibility(View.INVISIBLE); 
     btnright.setVisibility(View.INVISIBLE); 
     lblmaintext.setBackgroundResource(0); 

     timer = new CountDownTimer(4000,1000) { 
@Override 
public void onTick(long millisUntilFinished) { 
     lblmaintext.setTextSize(35); 

     lblmaintext.setText(arr[counter_countdown]); 
     counter_countdown++; 
     } 

@Override 
public void onFinish() { 
     btnleft.setVisibility(View.VISIBLE); 
     btnright.setVisibility(View.VISIBLE); 
     lblmaintext.setBackgroundResource(drawwable_amber); 
     // lblmaintext.setText(tanan[counter]); 
     //setImage(); 
     val_first_launch(); 
     timeRun = System.currentTimeMillis(); 

     react = true; 
     reacFaster(); 

     } 
     }.start(); 
     } 


public void reacFaster(){ 
     reactFaster_timer = new CountDownTimer(Global_variables.reaction_time_first_param,Global_variables.reaction_time_second_param) { 
@Override 
public void onTick(long millisUntilFinished) { 

     } 

@Override 
public void onFinish() { 
     if(react == true){ 
     //Toast.makeText(Place_to_go_1.this, "please react faster", Toast.LENGTH_SHORT).show(); 
     react_fast.start(); 
     lblreact.setVisibility(View.VISIBLE); 
     } 
     } 
     }.start(); 
     } 



public void populate_headers(String one,String two){ 
     //== this methos sets headers as random==// 
     headers = new ArrayList<String>(); 
     headers.add(one); 
     headers.add(two); 
     Collections.shuffle(headers); 
     p_one.setText(headers.get(0)); 
     p_two.setText(headers.get(1)); 
     } 
public void populate_attributes(){ 
     attributes = new ArrayList<String>(); 
     for(int h =0;h < 5;h++){ 
     attributes.add(placeone); 
     attributes.add(placetwo); 
     } 
     Collections.shuffle(attributes); 
     tanan = new String[attributes.size()]; 
     for(int k = 0; k < tanan.length;k++){ 
     tanan[k] = attributes.get(k); 
     } 
     } 

public void postEnd(){ 
     instruct = false; 
     lblprompt.setVisibility(View.INVISIBLE); 
     btnright.setVisibility(View.INVISIBLE); 
     btnleft.setVisibility(View.INVISIBLE); 
     next.setVisibility(View.VISIBLE); 
     lblmaintext.setBackgroundResource(0); 
     lblmaintext.setTextSize(20); 
     p_one.setVisibility(View.INVISIBLE); 
     p_two.setVisibility(View.INVISIBLE); 

     imgmain.setVisibility(View.INVISIBLE); 

     reactFaster_timer.cancel(); 
     lblreact.setVisibility(View.INVISIBLE); 
     lblmaintext.setText("Well done!\nNext, is the main task. It is exactly the same as before but this time words will appear on the screen that might distract you. \nPlease respond as quickly as you can.\n Press Next to begin"); 
     } 



//=========== validate if image is enabled/ disble if not set 
public void setImage_onLaunch(){ 
     if(Global_variables.getFirst_choice_image().equals("") || Global_variables.getSecond_choice_image().equals("")){ 
     disbaleImage(); 
     }else{ 

     } 
     } 

    public void setImage(){ 
     /* if(tanan[counter].equals(p_one.getText().toString())){ 
      imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getFirst_choice_image())); 
     }else{ 
      imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getSecond_choice_image())); 
     }*/ 
     if(placeone.equals(tanan[counter])){ 
      imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getFirst_choice_image())); 
     }else{ 
      imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getSecond_choice_image())); 
     } 
    } 


    public void val_first_launch(){ 
     if(Global_variables.getFirst_choice_image().equals("") || Global_variables.getSecond_choice_image().equals("")){ 
      lblmaintext.setVisibility(View.VISIBLE); 
      lblmaintext.setText(tanan[counter]); 
     }else{ 
      imgmain.setVisibility(View.VISIBLE); 
      if(placeone.equals(tanan[counter])){ 
       imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getFirst_choice_image())); 
      }else{ 
       imgmain.setImageBitmap(BitmapFactory.decodeFile(Global_variables.getSecond_choice_image())); 
      } 
     } 
    } 


    public void disbaleImage(){ 
     imgmain.setBackgroundResource(0); 
     imgmain.setVisibility(View.GONE); 
    } 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 

     startActivity(new Intent(getApplication(), MainActivity.class)); 
     finish(); 

    } 


    public String getreactionTime(String domain, String time){ 
     // Toast.makeText(Place_to_go_1.this, time, Toast.LENGTH_SHORT).show(); 
     //== get reaction time to every activity 
     Global_variables.set_timeStamps(domain, time); 
     return domain; 
    } 


    //===== prompt instruction==== 
    public void setInstruction(){ 
     btnleft.setVisibility(View.INVISIBLE); 
     btnright.setVisibility(View.INVISIBLE); 
     lblmaintext.setBackgroundResource(0); 
     lblmaintext.setTextSize(20); 
     lblmaintext.setText("Instruction:\n\nIf " + p_one.getText().toString() + " appears, press arrow left.\n If " + p_two.getText().toString() + 
       " appears, press arrow right.\n\nRespond as quickly as you can."); 
     next.setVisibility(View.VISIBLE); 
    } 
    //===== prompt instruction==== 


    public void wrong_click_audio(){ 

     wrong_press.start(); 


    } 

    //=============end class==================== 

    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

     // Checks the orientation of the screen 
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      // Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
     } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
      //Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 
+3

感谢您倾销所有代码。试试向我们展示您试过的东西 –

回答

1

这个想法是计算两个时间点之间的差异。我会写计算时间差的Java /测量反应时间在Java中的2个例子:

  1. System.nanoTime()或System.currentTimeMillis的()

的差异这里讨论:Time measuring overhead in Java

long endTimeNanoSec = 0; 
long startTimeNanoSec = System.nanoTime(); 
myWorkThatNeedsTiming(); // wait for user button press here 
endTimeNanoSec = System.nanoTime(); 

long totalWorkTimeNanos = endTimeNanoSec - startTimeNanoSec; 
  • 爪哇秒表
  • 的JavaDoc:StopWatch

    Stopwatch stopwatch = Stopwatch.createStarted(); 
    myWorkThatNeedsTiming(); // wait for user button press here 
    stopwatch.stop(); 
    
    long totalWorkTimeMillis = stopwatch.elapsedMillis(); 
    
    +0

    谢谢。你能帮我在哪里把代码on.please ..我只是新的Android。 :) – Crissy

    +0

    在你的代码中你有一个getreactionTime(...)方法。祝您在Android冒险中幸运! :) –

    +0

    好的。多亏了你:) – Crissy

    1

    这应有助于:

    尝试使用的onTouch代替onClick

    timeReaction是您想要的值。

    2

    这里计算反应时间的一个简单逻辑是创建一个变量,用于保存向用户弹出问题的时间,以及用户显示点击反应的时间以提问并计算这两个动作之间的时间差。

    long timeWhenQuestionShowed = System.currentTimeMillis(); 
    long timeWhenUserReacted = System.currentTimeMillis(); 
    long reactionTime = timeWhenQuestionShowed - timeWhenUserReacted; 
    
    +0

    这样的答案不会满足所需的条件,因为你正在按顺序执行两行,没有任何点击检查按钮。 – Talha

    +1

    它是一个逻辑我现在提出它直到你找出事件发生时间和计算时差。在堆栈中,我不认为只是放弃特定于某种情况的所有代码是有效的方式来学习 –

    +0

    这就是这个问题,不是吗? – Talha