计时器不允许用户编辑

时间:2016-10-23 20:34:56

标签: java android xml user-interface timer

我是一个初学者,我构建了一个计时器应用程序,我希望该应用程序允许用户编辑时间。喜欢在android内置计时器。我编写了代码并且它并没有真正起作用它的节目就像它可以编辑但是当我试图写一些东西时,它会改变一秒钟并回到它的状态。 ; s之前... ..

的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_stopwatch"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.adir.stopwatch.StopwatchActivity"
    android:background="#000000"

    >

    <Button
        android:id="@+id/reset_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickReset"
        android:text="@string/reset"
android:textColor="#FFFFFF"
        android:layout_below="@+id/stop_button"
        android:layout_alignLeft="@+id/stop_button"
        android:layout_alignStart="@+id/stop_button" />

    <EditText
        android:id="@+id/time_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="92sp"
        android:textColor="#FFFFFF"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"

android:inputType="time"
        />
    <Button
        android:id="@+id/start_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickStart"
android:text="@string/start"
        android:textColor="#FFFFFF"
        android:layout_below="@+id/time_view"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp" />

    <Button
        android:id="@+id/stop_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickStop"
        android:text="@string/stop"
        android:textColor="#FFFFFF"
        android:layout_alignLeft="@+id/start_button"
        android:layout_alignStart="@+id/start_button"
        android:layout_below="@+id/start_button"></Button>


</RelativeLayout>

爪哇:

public class StopwatchActivity extends Activity {
    private int seconds=0;
    private boolean running;
private boolean wasRunning;
public String time;
    public int hours=seconds/3600;
    public int minutes=(seconds%3600)/60;
    public int secs=seconds%60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stopwatch);
        if(savedInstanceState!=null){
            seconds=savedInstanceState.getInt("seconds");
            running=savedInstanceState.getBoolean("running");
            wasRunning=savedInstanceState.getBoolean("wasRunning");
        }
        runTimer();
    }

    public void onClickStart(View view) {

        running = true;
    }
    protected void onStop(){
        super.onStop();
        wasRunning=running;
        running=false;
    }

    protected void onStart() {
    super.onStart();
        if(wasRunning){
            running=true;
        }
    }

    public void onClickStop(View view) {
        running = false;


    }

    public void onClickReset(View view) {
        running = false;
        seconds = 0;
    }

    private void runTimer() {

        final EditText timeView = (EditText) findViewById(R.id.time_view);
        time=timeView.getText().toString();
        final Handler handler = new Handler();
        handler.post(new Runnable() {
            @Override
            public void run() {

                time = String.format("%d:%02d:%02d",hours, minutes, secs);

                timeView.setText(time);
                if (running) {
                    seconds--;
                }
                handler.postDelayed(this, 1000);
            }
        });


    }
public void onSaveInstanceState(Bundle savedInstanceState){
    savedInstanceState.putInt("seconds",seconds);
    savedInstanceState.putBoolean("running",running);
    savedInstanceState.putBoolean("wasRunning",wasRunning);
}

    protected void onPause(){
        super.onPause();
        wasRunning=running;
        running=false;
    }

    protected void onResume(){
        super.onResume();
        if(wasRunning){
            running=true;
        }
    }


}

我需要修理什么?

1 个答案:

答案 0 :(得分:0)

下面的代码可以让您了解该怎么做。单击该按钮时,它将获取EditText的文本并将其放入字符串中。然后它将转换为int。然后,您可以将新的int变量time放入CountDownTimer函数的第一个参数中,以设置计时器的长度。

   Button submit;
   TextView timerText;
   EditText editTextTime;
   CountdownTimer yourTimer;
   String  timeString;
   int time;

  submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

     timeString = String.valueOf(editTextTime.getText());
     time = Integer.valueOf(timeString);


        }
    });


    yourTimer = new CountDownTimer(time, 1000) {

    @Override
    public void onTick(long millisUntilFinished) {


        int seconds = (int) millisUntilFinished / 1000;
        int minutes = seconds / 60;
        seconds %= 60;
        minutes %= 60;


        if (seconds < 10 && seconds >= 1) {
            timerText.setText("" + seconds);
        }
    }

    @Override
    public void onFinish() {

    }
}.start();