0
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();
}
}

今度は XML ファイルです。textView1 は、ボタンがクリックされた回数です。timeElapsed はカウントダウン表示用です。問題は、カウントダウンタイマーを実装したときに表示されなかったということです。ディスプレイと背景の色が同じであるかどうかなど、すべての些細な間違いをチェックしました。すべてがうまく見えました。残念ながら、バックアップせずにカウントダウン タイマーのコードを削除しました。

<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>
4

2 に答える 2

0

あなたはこれをしたい..

  countValue = countValue++;

また

++countValue

使用した演算子はポストインクリメントです。つまり、最初に割り当ててからインクリメントします..したがって、値( 0 )が最初に割り当てられ、次にインクリメントされて失われます..変数値は永遠にゼロのままです..

于 2012-07-03T07:02:49.827 に答える
0

これを試して、

preIncrement方式++countValueまたは以下のように値をインクリメントする必要があります

  1. countValue = countValue + 1;
于 2012-07-03T09:16:08.587 に答える