如何有效地在一项活动上放置大量的秒表?

时间:2017-06-26 18:09:33

标签: java android

我制作了一个单一的秒表,但是我找不到在同一页面上添加几个秒表的方法。什么是有效的方法呢?我尝试复制代码并更改所有参数,但这似乎不起作用。

我现在的代码:

XML

<RelativeLayout
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp">


    <EditText
        android:id="@+id/topTextInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_toLeftOf="@+id/timeOne"
        android:layout_toStartOf="@+id/timeOne"
        android:width="300dp"
        android:elevation="1dp" />

    <TextView
        android:id="@+id/timeOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/topTextInput"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginEnd="13dp"
        android:layout_marginRight="13dp"
        android:layout_marginTop="50dp"
        android:clickable="false"
        android:elevation="4dp"
        android:text="00:00:00"
        android:textSize="24sp"
        android:visibility="visible" />

    <Button
        android:id="@+id/stopbuttonOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/topTextInput"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/topTextInput"
        android:layout_toEndOf="@+id/topTextInput"
        android:layout_toRightOf="@+id/topTextInput"
        android:width="110dp"
        android:background="@android:color/darker_gray"
        android:onClick="stopClick"
        android:text=""
        android:visibility="invisible" />

    <Button
        android:id="@+id/startbuttonOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/topTextInput"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/topTextInput"
        android:layout_toEndOf="@+id/topTextInput"
        android:layout_toRightOf="@+id/topTextInput"
        android:width="120dp"
        android:onClick="startClick"
        android:text=""
        android:visibility="visible" />

    <Button
        android:id="@+id/resetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Reset"
        android:onClick="resetClick" />

    <TextView
        android:id="@+id/millitimeOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/timeOne"
        android:visibility="visible" />


</RelativeLayout>

Java

package com.keur.joran.time;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
{

private TextView tempTextView; //Temporary TextView
private Button tempBtn; //Temporary Button
private Handler mHandler = new Handler();
private Handler m2Handler = new Handler();
private long startTime;
private long elapsedTime;
private long start2Time;
private long elapsed2Time;
private final int REFRESH_RATE = 100;
private String hours,minutes,seconds,milliseconds;
private long secs,mins,hrs,msecs;
private boolean stopped = false;
private Runnable startTimer = new Runnable() { public void run() { elapsedTime = System.currentTimeMillis() - startTime; updateTimer(elapsedTime); mHandler.postDelayed(this,REFRESH_RATE); } };
private Runnable start2Timer = new Runnable() { public void run() { elapsed2Time = System.currentTimeMillis() - start2Time; updateTimer(elapsed2Time); mHandler.postDelayed(this,REFRESH_RATE); } };

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

public void stopClick (View view){ hideStopButton(); mHandler.removeCallbacks(startTimer); stopped = true; }

public void startClick (View view){ showStopButton(); if(stopped){ startTime = System.currentTimeMillis() - elapsedTime; } else{ startTime = System.currentTimeMillis(); } mHandler.removeCallbacks(startTimer); mHandler.postDelayed(startTimer, 0); }

public void resetClick (View view){ stopped = false; ((TextView)findViewById(R.id.timeOne)).setText("00:00:00"); ((TextView)findViewById(R.id.millitimeOne)).setText(".0"); }

private void showStopButton(){ ((Button)findViewById(R.id.startbuttonOne)).setVisibility(View.GONE);
    ((Button)findViewById(R.id.stopbuttonOne)).setVisibility(View.VISIBLE);
}

private void hideStopButton(){ ((Button)findViewById(R.id.startbuttonOne)).setVisibility(View.VISIBLE);
    ((Button)findViewById(R.id.stopbuttonOne)).setVisibility(View.GONE);
}

private void updateTimer (float time){ secs = (long)(time/1000);
    mins = (long)((time/1000)/60); hrs = (long)(((time/1000)/60)/60);

    /* Convert the seconds to String * and format to ensure it has * a leading zero when required */
    secs = secs % 60;seconds=String.valueOf(secs);
    if(secs == 0){ seconds = "00"; } if(secs <10 && secs > 0){ seconds = "0"+seconds;
    }

    /* Convert the minutes to String and format the String */ mins = mins % 60; minutes=String.valueOf(mins); if(mins == 0){ minutes = "00"; } if(mins <10 && mins > 0){ minutes = "0"+minutes; } /* Convert the hours to String and format the String */ hours=String.valueOf(hrs); if(hrs == 0){ hours = "00"; } if(hrs <10 && hrs > 0){ hours = "0"+hours; } /* Although we are not using milliseconds on the timer in this example * I included the code in the event that you wanted to include it on your own */ milliseconds = String.valueOf((long)time); if(milliseconds.length()==2){ milliseconds = "0"+milliseconds; } if(milliseconds.length()<=1){ milliseconds = "00"; } milliseconds = milliseconds.substring(milliseconds.length()-3, milliseconds.length()-2); /* Setting the timer text to the elapsed time */ ((TextView)findViewById(R.id.timeOne)).setText(hours + ":" + minutes + ":" + seconds); ((TextView)findViewById(R.id.millitimeOne)).setText("." + milliseconds); }


}

1 个答案:

答案 0 :(得分:0)

将秒表转换为自定义视图或片段。然后你应该只需要在xml中包含view / fragment,并调用你在java代码中添加的相应的事件处理函数。

相关问题