Android 将“开始”按钮更改为“暂停”按钮

Android 将“开始”按钮更改为“暂停”按钮,android,Android,我想在我的应用程序中使用秒表。所以我尝试了这个教程,但我需要的是两个改变,我真的无法理解它们 我发现一个错误,当我按下开始,然后再次将再次启动。所以我想要的是一个开始按钮,按下时会变成暂停按钮,反之亦然。我如何切换按钮 我的按钮布局如下: <Button android:id=”@+id/btnPause” android:layout_width=”90dp” android:layout_marginLeft=”20dp” android:layout_height=”45dp” a

我想在我的应用程序中使用秒表。所以我尝试了这个教程,但我需要的是两个改变,我真的无法理解它们

我发现一个错误,当我按下开始,然后再次将再次启动。所以我想要的是一个开始按钮,按下时会变成暂停按钮,反之亦然。我如何切换按钮

我的按钮布局如下:

<Button

android:id=”@+id/btnPause”
android:layout_width=”90dp”
android:layout_marginLeft=”20dp”
android:layout_height=”45dp”
android:layout_centerVertical=”true”
android:layout_toRightOf=”@+id/btnStart”
android:text=”Pause” />

<Button
android:id=”@+id/btnStart”
android:layout_width=”90dp”
android:layout_height=”45dp”

android:layout_alignParentLeft=”true”
android:layout_centerVertical=”true”
android:layout_marginLeft=”68dp”
android:text=”Start” />
package com.androidituts.stopwatch;

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

public class StopwatchActivity extends Activity {
/** Called when the activity is first created. */
private TextView textTimer;
private Button startButton;
private Button pauseButton;
private long startTime = 0L;
private Handler myHandler = new Handler();
long timeInMillies = 0L;
long timeSwap = 0L;
long finalTime = 0L;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textTimer = (TextView) findViewById(R.id.textTimer);

startButton = (Button) findViewById(R.id.btnStart);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);

}
});

pauseButton = (Button) findViewById(R.id.btnPause);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);

}
});

}

private Runnable updateTimerMethod = new Runnable() {

public void run() {
timeInMillies = SystemClock.uptimeMillis() – startTime;
finalTime = timeSwap + timeInMillies;

int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText(“” + minutes + “:”
+ String.format(“%02d”, seconds) + “:”
+ String.format(“%03d”, milliseconds));
myHandler.postDelayed(this, 0);
}

};

}

您可以自己更改按钮的文本,或者您应该尝试使其中一个按钮可见,然后只需将可见性从
视图切换到
视图。可见
视图。消失
,然后对另一个按钮执行相反的操作

您可以尝试以下方法:

startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

if(startButton.getText().equalsIgnoreCase("Start")) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
}

else if(startButton.getText().equalsIgnoreCase("Pause")) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
}
}
});

我用一个按钮做了同样的事情,每次你按下它,它都会切换。我是这样做的

声明一个
int状态=1

状态是指按钮的功能和外观。假设
1
start
2
pause
3
stop
(仅用于教程目的)

设置默认状态(在本例中为1)并相应地设置背景

每次在
onclicklistener
中检查状态。如果状态为1,则执行相应的功能,将按钮背景更改为暂停,并将状态更改为2,其他按钮的状态也一样

祝你好运