Java 我在这段代码中嵌入了一个倒计时,但它没有
现在是XML文件。textView1表示按钮被单击的次数。时间流逝用于倒计时显示。问题是,当我实现一个倒计时时,它并没有显示出来。我检查了所有的小错误,比如显示器和背景的颜色是否相同;一切似乎都很好。遗憾的是,我删除了倒计时计时器的代码,但没有备份它Java 我在这段代码中嵌入了一个倒计时,但它没有,java,android,countdowntimer,post-increment,Java,Android,Countdowntimer,Post Increment,现在是XML文件。textView1表示按钮被单击的次数。时间流逝用于倒计时显示。问题是,当我实现一个倒计时时,它并没有显示出来。我检查了所有的小错误,比如显示器和背景的颜色是否相同;一切似乎都很好。遗憾的是,我删除了倒计时计时器的代码,但没有备份它 package com.android.tapme; import android.os.Bundle; import android.app.Activity; import android.widget.*; import android.v
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();
}
}
你想这么做
<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>
或
您使用的运算符是增量后运算符,即先赋值,然后增量。。因此,您的值0首先被赋值,然后递增并丢失。。您的变量值永远保持为零。试试这个
您必须以preIncrement fashion++countValue或以下方式递增该值
countValue=countValue+1;
你称之为倒计时…?这段代码有什么问题?您的代码段工作正常。也就是说,当我们按下“点击”按钮时,textview1文本将递增。但倒计时从何而来?嗯……这不是问题所在。水龙头计数器工作正常。我想知道如何在这里面嵌入倒计时。
countValue = countValue++;
++countValue