我嵌入到此code倒数计时器,但它没 [英] I embedded a countdown timer in this code, but it didn

查看:158
本文介绍了我嵌入到此code倒数计时器,但它没的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 包com.android.tapme;进口android.os.Bundle;
进口android.app.Activity;
导入android.widget *。
导入android.view *。公共类TapMe延伸活动{私人INT countValue = 0;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_tap_me);
    checkTapValue();
}
私人无效checkTapValue()
{
    按钮tapButton =(按钮)findViewById(R.id.tapButton);
    tapButton.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            countValue ++;
            TextView中的TextView;
            TextView的=(的TextView)findViewById(R.id.textView1);
            textView.setTextSize(40);
            textView.setText(Integer.toString(countValue));
        }
    });}
@覆盖
保护无效onResume()
{
    super.onResume();
    checkTapValue();
}
}

现在的XML文件。 textView1是次按钮被点击的数目。 timeElapsed是倒计时显示。事情是,当我实现了一个倒数计时器也没有显示。我检查了所有琐碎的错误,就像如果显示器和背景的颜色是相同的;一切似乎罚款。可悲的是,我删除了倒计时的code,而不进行备份。

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#000000><按钮
    机器人:ID =@ + ID / tapButton
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:layout_marginTop =186dp
    机器人:填充=15dp
    机器人:文字=@字符串/ tap_me
    机器人:TEXTSIZE =32dp/><的TextView
    机器人:ID =@ + ID / timeElapsed
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / tapButton
    机器人:layout_marginTop =14dp
    机器人:填充=@扪/ padding_medium
    机器人:文字颜色=#FFFFFF
    工具:上下文=。TapMe/>
<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / textView2
    机器人:layout_centerHorizo​​ntal =真
    机器人:填充=@扪/ padding_medium
    机器人:文字颜色=#FFFFFF
    工具:上下文=。TapMe/>< / RelativeLayout的>


解决方案

试试这个,

您必须增加在价值的 preIncrement 时尚 ++ countValue 或如下


  1. countValue = countValue + 1;

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>

解决方案

Try this,

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

  1. countValue = countValue + 1;

这篇关于我嵌入到此code倒数计时器,但它没的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆