如何让用户输入倒数计时器

时间:2017-07-24 12:25:05

标签: java android timer sharedpreferences

我制作一个倒计时java文件,它有一个按钮和倒数计时器,我希望用户在另一个新活动中输入计时器,并在共享偏好的帮助下保存它请帮助我,并提前感谢。 这里是timer.java文件代码

package com.cinegoes.www.daily10exercise;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by ever on 7/25/2017.
 */

public class timer extends Activity implements View.OnClickListener {

    private CountDownTimer countDownTimer;
    private boolean timerStarted = false;
    private Button buttonStart;
    public TextView textView;
    private final long startTime = 30 * 1000;
    private final long interval = 1 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timer);
        buttonStart = (Button) this.findViewById(R.id.button);
        buttonStart.setOnClickListener(this);
        textView = (TextView) this.findViewById(R.id.textView);
        countDownTimer = new CountDownTimerActivity(startTime, interval);
        textView.setText(textView.getText() + String.valueOf(startTime/1000));
    }

    @Override
    protected void onStart() {
        super.onStart();
        countDownTimer.start();
        timerStarted = true;
    }

    @Override
    public void onClick(View v) {
        finish();
    }


    public class CountDownTimerActivity extends CountDownTimer {
        public CountDownTimerActivity(long startTime, long interval) {
            super(startTime, interval);
        }

        @Override
        public void onFinish() {
            textView.setText("Time's up!");
            finish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            textView.setText("" + millisUntilFinished/1000);
        }
    }

}

这是我的timer.xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="10dip"
    android:textSize="50dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="105dp" />


<Button
    android:id="@+id/button"
    android:layout_width="140dp"
    android:layout_height="38dp"
    android:background="@drawable/m"
    android:onClick="onclick"
    android:text="skip"
    android:textColor="#ffffff"
    android:textSize="18sp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="85dp" />

</RelativeLayout>

0 个答案:

没有答案
相关问题