单击10次按钮后,在“警报对话框”中显示最终时间

时间:2014-06-24 03:57:58

标签: android alertdialog

现在我将我的应用程序设置为在单击10次按钮后显示警告对话框以及停止计时器。我无法弄清楚如何在警告对话框中显示计时器停止的最后时间。我想制作一个警告对话框,显示游戏结束,并显示玩家的最终时间。知道怎么做吗?如果可以,请帮忙。

JAVA CODE:

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;
import android.content.DialogInterface;


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 alertDialogBuilder = new                         AlertDialog.Builder(MainActivity.this)
                    .setTitle("Congratulations!")
                    .setMessage((R.id.timerValue))
                    .setIcon(R.drawable.ic_launcher)
                    .setCancelable(false)
                    .setPositiveButton(getString(R.string.play_again), new DialogInterface.OnClickListener() {              
                        public void onClick(DialogInterface dialog, int id) {   

                            dialog.cancel();}})
                    .setNegativeButton(getString(R.string.levels_menu), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();}});           

                AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();
            }
        }           
    });

}


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

    };


}

XML CODE:

<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="#FFFFFF"
        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="#FFFFFF"/>

</RelativeLayout>

3 个答案:

答案 0 :(得分:3)

你可以这样做

int i =0;   // declare this globally.

btn.setonclicklistener(new onClicklistener
    onclick(View v){
    i++;
    if(i==10){
        // show alert dialog 
    }
    else{
        // to do your stub here
    }
}

答案 1 :(得分:0)

您需要在AlertDialog构建器中使用set message方法。

.setTitle("Congratulations!")
.setMessage("Your time was: " + updatedTime)
.setIcon(R.drawable.ic_launcher)

答案 2 :(得分:0)

为什么不用这个? http://developer.android.com/reference/android/widget/TextClock.html

,您的代码会显示您使用:

.setMessage((R.id.timerValue))

R.id.viewName是指资源的十六进制ID。使用此方法不会设置textView中包含的值。 你可以尝试:

.setMessage(((TextView)R.id.timerValue).getText().toString())

或者您可以先声明文本视图,并在创建对话框时调用TextView.getText()。toString()。