当我激活开始时间时,我希望看到当我从移动横向从垂直变为水平,从水平变为垂直时,数字会增加。
障碍:
当显示的数字增加值时,我想将移动横向从垂直视图更改为水平视图。当我这样做时,数字会从头开始。
今天,我是Android的新手,下面的源代码并没有成功。
我错过了哪一部分?
--------------------------
class StopwatchActivity
package com.jfdimarzio.stopwatch4;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class StopwatchActivity extends AppCompatActivity {
private int seconds = 0;
private boolean running;
private boolean wasRunning;
@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();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putInt("seconds", seconds);
savedInstanceState.putBoolean("running", running);
savedInstanceState.putBoolean("wasRunning", wasRunning);
}
@Override
protected void onStop()
{
super.onStop();
wasRunning = running;
running = false;
}
@Override
protected void onStart()
{
super.onStart();
if(wasRunning)
{
running = true;
}
}
@Override
protected void onPause()
{
super.onPause();
wasRunning = running;
running = false;
}
@Override
protected void onResume()
{
super.onResume();
if (wasRunning) {
running = true;
}
}
public void onClickStart(View view)
{
running = true;
}
public void onClickStop(View view)
{
running = false;
}
public void onClickReset(View view)
{
running = false;
seconds= 0;
}
private void runTimer()
{
final TextView timeView = (TextView)findViewById(R.id.time_view);
final Handler handler = new Handler();
handler.post(new Runnable()
{
@Override
public void run()
{
int hours = seconds/3600;
int minutes = (seconds%3600)/60;
int secs = seconds%60;
String time = String.format("%d:%02d:%02d", hours, minutes, secs);
timeView.setText(time);
if(running)
{
seconds++;
}
handler.postDelayed(this, 1000);
}
});
}
}
------------------------------
activity_stopwatch.xml
<?xml version="1.0" encoding="utf-8"?>
<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.jfdimarzio.stopwatch4.StopwatchActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView" />
<TextView
android:id="@+id/time_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="92sp" />
<Button
android:id="@+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/time_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:onClick="onClickStart"
android:text="@string/start" />
<Button
android:id="@+id/stop_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/start_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:onClick="onClickStop"
android:text="@string/stop" />
<Button
android:id="@+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/stop_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:onClick="onClickReset"
android:text="@string/reset" />
</RelativeLayout>
------------------------------
strings.xml
<resources>
<string name="app_name">Stopwatch4</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="reset">Reset</string>
</resources>
答案 0 :(得分:0)
您使用savedInstanceState.putInt(&#34; s econds&#34;)但savedInstanceState.getInt(&#34; S econds&#34;); 改变&#34;秒&#34;到&#34;秒&#34;在onCreate()中。
希望有这个帮助,我是StackOverFlow的新手。
Recursive:
1 3 5 8 12 51
Iterative:
1 3 5 8 12 51
Iterative:
1 3 5 8 12 37 51
Iterative:
1 2 3 5 8 12 37 51
Iterative:
1 2 3 5 8 12 19 37 51
Iterative:
1 2 3 5 8 9 12 19 37 51
Iterative:
1 2 3 5 7 8 9 12 19 37 51
Iterative:
1 2 3 5 7 8 9 12 19 37 41 51