永远不会使用方法'pauseChronometer(android.view.View)'

时间:2019-03-11 18:04:30

标签: java android

我的android studio项目上有一个计时器,但出现错误Method 'pauseChronometer(android.view.View)' is never used,我不知道这里出了什么问题。我已将代码添加到此文档中。该代码是用作秒表的计时器和每次单击按钮都会增加的计数器。唯一错误的是计时器。谢谢

Mainactivity.java

package com.rhdev.counter;


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

public class MainActivity extends AppCompatActivity {
    private Chronometer chronometer;
    private boolean running;
    private long pauseOffset;
    private int mCounter = 0;
    Button btn;
    Button btn2;
    TextView txv;

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

        chronometer = findViewById(R.id.chronometer);

        btn = (Button) findViewById(R.id.bt);
        btn2 = (Button) findViewById(R.id.bt2);
        txv = (TextView) findViewById(R.id.tx);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCounter++;
                txv.setText(Integer.toString(mCounter));
            }

        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCounter--;
                txv.setText(Integer.toString(mCounter));


            }


            public void startChronometer(android.view.View) {
                if (!running) {
                    chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
                    chronometer.start();
                    running = true;
                }

            }

            public void pauseChronometer(View v) {
                if (running) {
                    chronometer.stop();
                    pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
                    running = false;
                }

            }

            public void resetChronometer(View v) {
                chronometer.setBase(SystemClock.elapsedRealtime());
                pauseOffset = 0;

            }
        });

    }

}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/bt"
        android:layout_width="272dp"
        android:layout_height="272dp"
        android:layout_marginStart="37dp"
        android:layout_marginLeft="37dp"
        android:layout_marginEnd="38dp"
        android:layout_marginRight="38dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/btn_background"
        android:text="@string/_1_lap"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/bt2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/tx"
        android:layout_width="379dp"
        android:layout_height="125dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="29dp"
        android:text="@string/_0"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="100sp"
        app:layout_constraintBottom_toTopOf="@+id/bt"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />


    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="108dp"
        android:layout_marginLeft="108dp"
        android:layout_marginEnd="104dp"
        android:layout_marginRight="104dp"
        android:textColor="@android:color/black"
        android:textSize="60sp"
        app:layout_constraintBottom_toTopOf="@+id/tx"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/button2"
        android:layout_width="190dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark"
        android:onClick="pauseChronometer"
        android:text="@string/pause"
        android:textAppearance="@style/TextAppearance.AppCompat"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintStart_toEndOf="@+id/button" />


    <Button
        android:id="@+id/button"
        android:layout_width="108dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dp"
        android:background="@android:color/holo_blue_dark"
        android:onClick="startChronometer"
        android:text="@string/start"
        android:textAppearance="@style/TextAppearance.AppCompat"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent" />


    <Button
        android:id="@+id/button4"
        android:layout_width="115dp"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark"
        android:onClick="resetChronometer"
        android:text="@string/stop"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button2" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="162dp"
        android:layout_marginLeft="162dp"
        android:layout_marginEnd="162dp"
        android:layout_marginRight="162dp"
        android:layout_marginBottom="116dp"
        android:background="@android:color/holo_blue_light"
        android:text="@string/__1"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent" />


</android.support.constraint.ConstraintLayout>

0 个答案:

没有答案