I embedded a countdown timer in this code, but it didn

你离开我真会死。 提交于 2019-12-13 02:41:50

问题


package com.android.tapme;

import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.*;

public class TapMe extends Activity {

private int countValue=0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tap_me);
    checkTapValue();
}
private void checkTapValue()
{
    Button tapButton=(Button)findViewById(R.id.tapButton);
    tapButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            countValue++;
            TextView textView;
            textView = (TextView) findViewById(R.id.textView1);
            textView.setTextSize(40);
            textView.setText(Integer.toString(countValue)); 
        }
    });     

}
@Override
protected void onResume()
{
    super.onResume();
    checkTapValue();
}
}

Now the XML file. textView1 is for the number of times the button has been clicked. timeElapsed is for the countdown display. Thing is when I implemented a countdown timer it didn't display. I checked for all trivial mistakes, like if the colors of the display and background were same; everything seemed fine. Sadly, I removed the code of the countdown timer without backing it up.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" >

<Button
    android:id="@+id/tapButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="186dp"
    android:padding="15dp"
    android:text="@string/tap_me"
    android:textSize="32dp" />

<TextView
    android:id="@+id/timeElapsed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tapButton"
    android:layout_marginTop="14dp"
    android:padding="@dimen/padding_medium"
    android:textColor="#FFFFFF"
    tools:context=".TapMe" />


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:padding="@dimen/padding_medium"
    android:textColor="#FFFFFF"
    tools:context=".TapMe" />

</RelativeLayout>

回答1:


you want to do this..

  countValue = countValue++;

or

++countValue

the operatoer you used is post increment, that is assign first then increment.. so your value ( 0 ) gets assigned first and then it is getting incremented and lost.. your variable value remains zero for ever..




回答2:


Try this,

You must increment the value in preIncrement fashion ++countValue or as below

  1. countValue = countValue + 1;


来源:https://stackoverflow.com/questions/11305930/i-embedded-a-countdown-timer-in-this-code-but-it-didn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!