如何在点击10次按钮后显示AlertDialog的时间

时间:2014-06-10 00:34:45

标签: android timer counter alertdialog onclicklistener

所以我有这个简单的应用程序,我试图在最快的时间内点击按钮10次。我想点击10次后显示一个AlertDialog并让该对话框显示你的时间,并有2个按钮允许用户再次播放,这将重置游戏或返回应用程序的主菜单。我似乎无法弄清楚如何编码AlertDialog或者我是否将它放在正确的位置。如果可以,请帮忙。

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="#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 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;


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

    };


}

2 个答案:

答案 0 :(得分:0)

你有什么问题?您的AlertDialog状态良好,但您可能希望为onClickListener按钮设置AlertDialog。以下是为肯定按钮设置onClickListener的示例:

    // Build an alert dialog
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this)
            .setTitle(getString(R.string.dialog_title))
            .setMessage(getString(R.string.dialog_message))
            .setCancelable(false)
            .setPositiveButton(getString(R.string.dialog_close_button), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // Close the dialog
                    dialog.cancel();
                }
              });

    // Create the alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();

    // Show the alert dialog
    alertDialog.show();

答案 1 :(得分:0)

    AlertDialog alertDialog = new AlertDialog.Builder(
                            YOUR_CLASS.this).create();

            // Setting Dialog Title
            alertDialog.setTitle("Alert Dialog");

            // Setting Dialog Message
            alertDialog.setMessage("HELLO ANDROID");

            // Setting Icon to Dialog
            alertDialog.setIcon(R.drawable.ic_launcher);

            // Setting Positive "Yes" Button
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {

        // Write your code here to invoke YES event
        Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // Write your code here to invoke NO event
        Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
        dialog.cancel();
        }
    });

            // Showing Alert Message
            alertDialog.show();