在我的应用程序中我有一个从15开始的计数器,我有一个动画和声音的按钮,问题是我要关闭声音和按钮的动画并更改背景播放当计数器达到0时,点击另一个声音
这是我试图锁定电表和声音的代码,但它不起作用,我使用它错了你有什么想法吗?
代码:
MediaPlayer mediaPlayer;
int contatore;
TextView Display;
Button b1;
private StartAppAd startAppAd = new StartAppAd(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.b1);
contatore = 15;
b1 = (Button)findViewById(R.id.b1);
Display = (TextView)findViewById(R.id.contatore);
final Animation animRotate = AnimationUtils.loadAnimation(this, R.anim.pistola);
b1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
contatore --;
Display.setText(""+contatore);
for (int contatore = 0; contatore!= 0; ){
contatore = (Integer) null;
mediaPlayer = null;
}
arg0.startAnimation(animRotate);
final Button button1 = (Button)findViewById(R.id.b1);
button1.setBackgroundResource(R.drawable.deserteagle_1);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
button1.setBackgroundResource(R.drawable.deserteagle_0);
}
}, 100);
mediaPlayer = MediaPlayer.create(getBaseContext(),R.raw.b1);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// TODO Auto-generated method stub
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
};
});
}
});
}
});
提前致谢
答案 0 :(得分:0)
好的......从查看代码,尝试这些更改。
此:
final Button button1 = (Button)findViewById(R.id.b1);
button1.setBackgroundResource(R.drawable.deserteagle_1);
对此:
runOnUiThread(new Runnable() {
// you already have `b1` that points to `R.id.b1`, so use that
// instead of using `findViewById` again
b1.setBackgroundResource(...);
});
我不确定这部分应该做什么:
for (int contatore = 0; contatore!= 0; ){
contatore = (Integer) null;
mediaPlayer = null;
}
还要确保将Display.setText
包裹在runOnUiThread
答案 1 :(得分:0)
我用这种方式解决了计数器的问题,现在只剩下让我播放另一个声音而不是这个并停止动画并改变按钮的背景
private int contatore = 15;
private Object mediaPlayer;
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
contatore --;
Display.setText(""+contatore);
if(this.contatore <= 0)
{
this.contatore=1;
this.mediaPlayer = null;
}