Countdown

J

Jajobe

Erfahrenes Mitglied
14
Guten Tag zusammen,
Ich programmiere gerade ein Spiel bei dem man ein Button drückt und sich eine Progressbar füllt. Gleichzeitig geht ein Countdown von 10 abwärts, sobald man den Button drückt. Wenn der Countdown dann auf 0 ist, soll nun angezeigt werden wie oft der Button gedrückt wurde und das nochmal auf dem Button drücken soll ebenfalls unterdrückt werden.
Nur ich bekomme es auf Teufel komm raus nicht auf die Reihe :sad:
Kann einer helfen?

Anmerkung: Ich weiß, dass ich die Buttons und Textviews ohne Strings beschriftet hatte.

xml:
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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="com.bjcreative.kleinesspiel.MainActivity" >

    <ProgressBar
        android:id="@+id/progressBar1"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="wrap_content"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:minHeight="50dp"
        android:minWidth="200dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Drücke" />
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="Timer" />

</LinearLayout>



Source Code:

Code:
package com.bjcreative.buttondrueck;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class GameActivity extends Activity implements OnClickListener {

	
	ProgressBar bar;
	Button btn;
	TextView tv;
	int i = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		setContentView(R.layout.activity_game);
		
		tv  = (TextView)findViewById(R.id.textView1);
        tv.setText("10"); // startting from 10.
 
        final MyCounter timer = new MyCounter(10000,1000);
        
       
            
         
                timer.start();
            
       
       
    }
 
    public class MyCounter extends CountDownTimer{
 
        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
 
        @Override
        public void onFinish() {
            System.out.println("Timer Completed.");
            tv.setText("Timer Completed.");
            
        }
 
        @Override
        public void onTick(long millisUntilFinished) {
            tv.setText((millisUntilFinished/1000)+"");
            System.out.println("Timer  : " + (millisUntilFinished/1000));
        }
		
		
		
	
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.game, menu);
		

		bar = (ProgressBar)findViewById(R.id.progressBar1);
		bar.setMax(200);
		
		btn = (Button)findViewById(R.id.button1);
		btn.setOnClickListener(this);
		
		
		
		return true;
	}
	
	public void onClick(View e){
		if(e == btn){
			i += 4;
			
			if(i >= 200){
				AlertDialog ad = new AlertDialog.Builder(this).create();
				ad.setMessage("Du hast die Leiste voll gekriegt");
				ad.show();
				
				i = 0;
			}
			
			bar.setProgress(i);
		}
	
		
	}

}
 
Du meinst, du willst das in etwa so?:

Code:
        public void onFinish() {
            tv.setText("Timer Completed. Gedrückt: " + i / 4);
            btn.setOnClickListener(null);
        }
 
  • Danke
Reaktionen: Jajobe
tuppy schrieb:
Du meinst, du willst das in etwa so?:

Code:
        public void onFinish() {
            tv.setText("Timer Completed. Gedrückt: " + i / 4);
            btn.setOnClickListener(null);
        }

Hey funktioniert super. Warum bin ich selbst nicht darauf gekommen?!:confused2:
Danke nochmal dafür.
 
Zuletzt bearbeitet:

Ähnliche Themen

H
Antworten
2
Aufrufe
1.125
heikoj10.1
H
H
  • Hoerti
Antworten
5
Aufrufe
675
DagobertDokate
DagobertDokate
2
  • 2slow4you
Antworten
1
Aufrufe
880
amfa
A
Zurück
Oben Unten