【发布时间】:2014-06-10 00:34:44
【问题描述】:
所以我制作了这个简单的应用程序,您可以在其中尝试以最快的速度点击一个按钮 10 次。我想在您单击 10 次后显示一个 AlertDialog 并让该对话框显示您的时间并有 2 个按钮允许用户再次玩,这将重置游戏或返回应用程序的主菜单。我似乎无法弄清楚如何对 AlertDialog 进行编码,或者我什至无法将它放在正确的位置。如果可以的话,请帮忙。
XML 代码:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/thirty_tap_game_background">
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="138dp"
android:background="@drawable/test_play_button" />
<TextView
android:id="@+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:textSize="40sp"
android:textColor="#FF0000"
android:text="@string/timerVal" />
<TextView
android:id="@+id/TextViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="73dp"
android:text="@string/countVal"
android:textSize="30sp"
android:textColor="#FF0000"/>
</RelativeLayout>
JAVA 代码:
package com.example.thirtytapgametest;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
public class MainActivity extends Activity {
private int mCount = 0;
private ImageButton startbutton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
timerValue = (TextView) findViewById(R.id.timerValue);
startbutton = (ImageButton) findViewById(R.id.imageButton1);
startbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mCount ==0) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
mCount++;
countTextView.setText("Taps: " + mCount);
if(mCount == 10) {
view.setEnabled(false);
customHandler.removeCallbacks(updateTimerThread);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Game Over!");
alert.setMessage("Your Score is:");
alert.setPositiveButton("PlayAgain", null);
alert.setNegativeButton("Levels Menu", null);
AlertDialog alt= alert.create();
}
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
updatedTime = SystemClock.uptimeMillis() - startTime;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("Time: " + "" + mins + ":"
+String.format("%02d", secs) + ":"
+String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
}
【问题讨论】:
-
这里有什么错误吗?
-
现在发生了什么?
标签: android timer counter android-alertdialog onclicklistener