0

さて、画面に乱数がポップアップするアプリケーションを楽しみのために作成しています。乱数と同じ数字のボタンをタップするとポイントが入り、間違った数字のボタンをタップするとポイントを失います。問題は、十分なポイントを獲得するのに 10 秒しかないことです。とにかく、アプリケーションには2つの主な問題があります。正しいボタンをタップしてもスコアは上がりません。ただし、間違ったボタンをタップするとスコアが下がってしまいます。それは非常に奇妙だと思います。また、タイマーが切れた後にコンテンツ ビューを設定して、プレイヤーが勝ったかどうかを伝えるのに問題があります。新しいコンテンツ ビューを設定するには、別のアクティビティを作成する必要がありますか? それとも別のエラーがありますか?

ご協力ありがとうございました!

これが私のアクティビティクラスです:

package ab.game.crazynumbers;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Level1Activity extends Activity {

Random r;
Timer t;
int random;
double time;
boolean running = false;
TextView showingnumber;
TextView score;
TextView timer;
Handler handler;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
Button b7;
Button b8;
Button b9;
Button b0;
int playerscore;
TextView finalscore;
TextView finalscore2;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.level_1);

    playerscore = 0;

    showingnumber = (TextView) findViewById(R.id.game_number);
    score = (TextView) findViewById(R.id.game_score);
    timer = (TextView) findViewById(R.id.game_timer);
    finalscore = (TextView) findViewById(R.id.finalscore);
    finalscore2 = (TextView) findViewById(R.id.finalscore2);

    running = true;
    time = 10.0;

    b0 = (Button) findViewById(R.id.b1_0);
    b1 = (Button) findViewById(R.id.b1_1);
    b2 = (Button) findViewById(R.id.b1_2);
    b3 = (Button) findViewById(R.id.b1_3);
    b4 = (Button) findViewById(R.id.b1_4);
    b5 = (Button) findViewById(R.id.b1_5);
    b6 = (Button) findViewById(R.id.b1_6);
    b7 = (Button) findViewById(R.id.b1_7);
    b8 = (Button) findViewById(R.id.b1_8);
    b9 = (Button) findViewById(R.id.b1_9);

    randomnumber();

    b1.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (1 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (1 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;

        }
    });

    b2.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (2 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (2 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    b3.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (3 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (3 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    b4.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (4 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (4 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    b5.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (5 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (5 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    b6.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (6 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (6 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    b7.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (7 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (7 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    b8.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (8 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (8 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    b9.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (9 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (9 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    b0.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent e) {

            if (e.getAction() == android.view.MotionEvent.ACTION_UP) {

                if (0 == random) {
                    playerscore++;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }

                if (0 != random) {
                    playerscore--;
                    runOnUiThread(new Runnable() {
                        public void run() {
                            score.setText("Your Score Is " + playerscore);
                        }
                    });
                    randomnumber();
                }
            }

            return false;
        }
    });

    startTimer();

}

public void startTimer() {

    Timer t = new Timer("Game Timer");

    handler = new Handler();

    t.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {

            if (time > 0) {

                time -= 0.1;

                time = Math.round(time * 10.0) / 10.0;

                runOnUiThread(new Runnable() {
                    public void run() {
                        timer.setText("Seconds Remaining: " + time);
                    }
                });

            }

            if (time <= 0) {
                this.cancel();
                if (playerscore >= 10) {
                    setContentView(R.layout.win);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            finalscore.setText("Your Final Score Is "
                                    + playerscore);
                        }
                    });
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if (playerscore < 10) {
                    setContentView(R.layout.lose);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            finalscore2.setText("Your Final Score Is "
                                    + playerscore);
                        }
                    });
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                finish();
            }

        }

    },

    // time before start of timer
            0,

            // time before it runs again
            100);

}

public void randomnumber() {

    r = new Random();
    random = r.nextInt(10);

    runOnUiThread(new Runnable() {
        public void run() {
            showingnumber.setText(String.valueOf(random));
        }
    });

}

}

ここに私のXMLがあります:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/game_timer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="Seconds Remaining: 0.0"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/game_score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="Your Score Is 0"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/b1.4"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignLeft="@+id/b1.1"
    android:layout_alignTop="@+id/b1.1"
    android:layout_marginTop="73dp"
    android:text="4" />

<TextView
    android:id="@+id/game_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/game_timer"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="89dp"
    android:gravity="center"
    android:text="0"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/b1.1"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/game_number"
    android:layout_marginLeft="14dp"
    android:layout_marginTop="130dp"
    android:text="1" />

<Button
    android:id="@+id/b1.7"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_above="@+id/game_score"
    android:layout_alignLeft="@+id/b1.4"
    android:layout_marginBottom="15dp"
    android:text="7" />

<Button
    android:id="@+id/b1.2"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/b1.1"
    android:layout_alignBottom="@+id/b1.1"
    android:layout_centerHorizontal="true"
    android:text="2" />

<Button
    android:id="@+id/b1.5"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/b1.4"
    android:layout_alignBottom="@+id/b1.4"
    android:layout_centerHorizontal="true"
    android:text="5" />

<Button
    android:id="@+id/b1.8"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/b1.7"
    android:layout_alignBottom="@+id/b1.7"
    android:layout_centerHorizontal="true"
    android:text="8" />

<Button
    android:id="@+id/b1.0"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_above="@+id/b1.2"
    android:layout_alignLeft="@+id/b1.2"
    android:layout_marginBottom="18dp"
    android:text="0" />

<Button
    android:id="@+id/b1.6"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/b1.4"
    android:layout_alignBottom="@+id/b1.4"
    android:layout_alignParentRight="true"
    android:layout_marginRight="20dp"
    android:text="6" />

<Button
    android:id="@+id/b1.9"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/b1.7"
    android:layout_alignBottom="@+id/b1.7"
    android:layout_alignLeft="@+id/b1.6"
    android:text="9" />

<Button
    android:id="@+id/b1.3"
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:layout_alignBaseline="@+id/b1.1"
    android:layout_alignBottom="@+id/b1.1"
    android:layout_alignLeft="@+id/b1.6"
    android:text="3" />

</RelativeLayout>
4

3 に答える 3

0

私はあなたのコードを見たことがありますが、あなたのコードに 2 つの問題が見つかりました。1.リスナーを抽出する必要があります。ボタンごとに記述するのは非常にばかげています。2. Android では、別のスレッドで UI を操作することは許可されていません。Handle を使用する必要があります。

私は中国人で、私の英語はとても下手です

于 2013-05-20T01:39:07.350 に答える
0

使い方が間違っていifます。ボタンを正しく選択した後、次の番号を生成し、もう一度チェックするかどうかを確認しますrandom!=that_button_number。ほぼ毎回発生するため、スコアが低下します。

     if (random == 3) {randomNumber()}else{}

いいえ

if(random==3){
      randomNumber();
  };     
if(random!=3){};

setContentViewアプリケーションの実行時に呼び出すことはできません。あなたは得るでしょうRuntimeException。あなたはできる:

  • 1 つのレイアウトを非表示にし、新しいものを表示setVisibility
  • 使用するFrame layout
  • フラグメントトランザクションや新しいビューの追加など、他の多くのもの
于 2013-05-20T01:45:16.440 に答える
0

スコアが増加した直後に乱数を生成しているため、スコアは上昇しますが、次のヒット後に再び減少しますif

if (1 == random) {
                playerscore++;
                runOnUiThread(new Runnable() {
                    public void run() {
                        score.setText("Your Score Is " + playerscore);
                    }
                });
                randomnumber();  // there is a new nunmber
            }

            if (1 != random) {  // this should be an else
                playerscore--;
                runOnUiThread(new Runnable() {
                    public void run() {
                        score.setText("Your Score Is " + playerscore);
                    }
                });
                randomnumber();

問題が発生している上記のコードにコメントを入れました

また、タイマーが切れた後にコンテンツ ビューを設定して、プレイヤーが勝ったかどうかを伝えるのに問題があります。新しいコンテンツ ビューを設定するには、別のアクティビティを作成する必要がありますか? それとも別のエラーがありますか?

どのような問題を抱えているかを知らずに、言うのは難しいです。しかし、これは可能です。ただし、これには AlertDialog を使用することをお勧めます。ActivitythemeDialogmanifest

Buttonsまた、それらはすべてほとんど同じことを行うため、すべてに対して1つのリスナーのみを使用できます.xmlでも、onClick関数をすべて同じに設定することによって

<Button
    android:id="@+id/button1
    ...
    android:onClick="functionName"/>

次にJavaで

public void functionName(View v)
{
     // v is the button that was clicked
}

またはJavaでリスナーを実装することにより、ここではonClickで

button1.setOnClickListener(this);  // do this for each
public void onClick(View v)
{
    // v is still the button clicked
}

次に、クリックされたボタンのテキストを取得し、乱数と比較します

于 2013-05-20T01:49:21.673 に答える