java - Android:如何创建 1 秒的计时器

标签 java android

好的,我正在开发一个andoird应用程序,即游戏simon,当我从菜单移动到游戏时,我遇到了一个问题。 我希望游戏在游戏开始时播放 3 种随机选择的声音 和应用程序在 1 秒延迟内一个接一个地播放它们,但问题是应用程序在游戏背景加载之前所有声音都同时播放..很快一切都不起作用,我很绝望。 为了使用这些按钮,我创建了一个名为“column”的类,其中包含 View 中的按钮及其发出的声音,并使用方法 play 来激活声音并更改图像。 游戏逻辑类 Game 这是代码:

列类别:

package com.example.thegreatsimon;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class Column {

ImageButton button;
SoundPool sound;
int soundId;    
int srcColor,lightColor;
Context con;
public Column(ImageButton button,Context con)
{
    sound=new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    this.button=button;
    switch (button.getId())
    {   
    case R.id.ibBlue:
    {
        soundId=sound.load(con,R.raw.a ,1);
        srcColor=R.drawable.blue;
        lightColor=R.drawable.lightblue;
    } break;
    case R.id.ibGreen:
    {
        soundId=sound.load(con,R.raw.b,1);
        srcColor=R.drawable.green;
        lightColor=R.drawable.lightgreen;
    } break;
    case R.id.ibRed:
    {
        soundId=sound.load(con,R.raw.c ,1);
        srcColor=R.drawable.red;
        lightColor=R.drawable.lightred;
    } break;
    case R.id.ibYellow:
    {
        soundId=sound.load(con,R.raw.d ,1);
        srcColor=R.drawable.yellow;
        lightColor=R.drawable.lightyellow;
    } break;
    }   
    button.setOnClickListener(new Listen());
}   

class Listen implements OnClickListener
{
    @Override
    public void onClick(final View v) 
    {
        play();
    }   
}

void play()
{
    sound.play(soundId, 1, 1, 0, 0, 1)  ;
    button.setBackgroundResource(lightColor);
    Handler handler = new Handler();    
    handler.postDelayed(new Runnable() 
    {
        @Override
        public void run() 
        {
            button.setBackgroundResource(srcColor);
        }
    }, 300);

}

}   

游戏类:

package com.example.thegreatsimon;
import java.util.ArrayList;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageButton;

public class Game extends Activity {

Column bYellow,bRed,bGreen,bBlue;
SoundPool a=new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
int diff;
int life=3;
Random rand= new Random();
Handler hand = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    diff = Settings.difficulty;
    switch(diff)
    {
    case(0):
        setContentView(R.layout.gameeasy);
    break;
    case(1):
        setContentView(R.layout.game);
    bYellow=new Column((ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
    break;
    case(2):
        setContentView(R.layout.game);
    bYellow=new      Column((ImageButton)findViewById(R.id.ibYellow),getApplicationContext());
    break;
    }
    bRed=new Column((ImageButton)findViewById(R.id.ibRed),getApplicationContext());
    bGreen=new Column((ImageButton)findViewById(R.id.ibGreen),getApplicationContext());
    bBlue=new Column((ImageButton)findViewById(R.id.ibBlue),getApplicationContext());
    EasyGame();
}


public boolean EasyGame()
{
    int r;
    ArrayList list = new ArrayList();
    boolean proceed;
    for(int i=0;i<3;i++)
    {
        r=rand.nextInt(3);
        switch(r)
        {
        case 0:
        {
            list.add(bRed);
            Sleep(bRed);                
        }break;
        case 1:
        {
            list.add(bGreen);
            Sleep(bGreen);
        }break;
        case 2:
        {
            list.add(bBlue);
            Sleep(bBlue);
        }break;
        }
    }
    proceed=UserPlay();

    /*for(int i=0;i<2;i++)
    {
        r=rand.nextInt(3);
        switch(r)
        {
        case 0:
            list.add(bRed);
            break;
        case 1:
            list.add(bGreen);
            break;
        case 2:
            list.add(bBlue);
            break;
        }
        for(int j=0;j<list.size();j++)
        {
            ((Column)list.get(j)).play();
            Sleep();
        }
        proceed=UserPlay();
    }*/

    return true;
}

public void MediumGame()
{

}
public void HardGame()
{

}



    public void Sleep(final Column c)
{
    Handler handler = new Handler();    
    handler.postDelayed(new Runnable() {
    @Override
                public void run() {
                   c.play();
                }
            }, 1000);
}



public boolean UserPlay()
{
    return true;
}

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

}

最佳答案

Handler handler = new Handler();    
handler.postDelayed(new Runnable() {
@Override
            public void run() {
               //write your code here to be executed after 1 second
            }
        }, 1000);

关于java - Android:如何创建 1 秒的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20029755/

相关文章:

java - 为什么我不能在我的 IntelliJ 中下载 OpenJDK-17?

android - 通过点击 Android 手机中的位置来获取谷歌地图中的位置名称

java - getGSMSignalStrength() 总是返回 99

java - 如何动态地向 GWT UiBinder 中的 HTML 元素添加类?

android - 如何仅为 1 个 fragment 设置后退箭头代替汉堡包图标?

Android OnLongClickListener 奇怪/不可靠的行为

Android Facebook 共享和墙贴在 Webview 中使用共享 url 不起作用

android - 内部类中的最终变量问题

java - 如何排除/storage/emulated/0/Ringtones中的文件

java - Java 中的注解