在我的程序的一部分在xml文件「activity_exam_class_1」中有一個名爲「imageButton_sound」的按鈕。我想播放聲音點擊此按鈕。首先當我點擊它,它播放聲音,但從第二次不起作用。按鈕被點擊一次後被禁用。我想重複使用
我在「Exam_class_1.java」文件中初始化這個xml文件。
我的程序的順序是 - 首先我會按下「imageButton_sound」,然後按相同文件的任何其他按鈕,如「imageButton_a」,「imageButton_b」「imageButton_c」...........等等 然後,在完成某些條件後,程序將轉到「success_exam.xml」或「fail_exam.xml」文件,這兩個文件都包含一個按鈕。按下按鈕後,它將返回到「activity_exam_class_1」,然後我會再次按下「imageButton_sound」,過程將繼續。 這裏是我的代碼: Exam_class_1.java:
package com.example.alphabet_school;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
public class Exam_class_1 extends Activity implements View.OnClickListener{
ImageButton im_a,im_b,im_c,im_d,im_e,im_f;
ImageButton im_success,im_fail;
ImageButton sound;
MediaPlayer joy_sound,wrong_sound,letter_sound;
int exam_sound,success=0,fail=0;
//int[] keepSound={R.raw.a,R.raw.b,R.raw.c,R.raw.d,R.raw.e,R.raw.f};
int ks=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exam_class_1);
sound=(ImageButton) findViewById(R.id.imageButton_sound);
im_a=(ImageButton) findViewById(R.id.imageButton_a);
im_b=(ImageButton) findViewById(R.id.imageButton_b);
im_c=(ImageButton) findViewById(R.id.imageButton_c);
im_d=(ImageButton) findViewById(R.id.imageButton_d);
im_e=(ImageButton) findViewById(R.id.imageButton_e);
im_f=(ImageButton) findViewById(R.id.imageButton_f);
for(int i=0;i<6;i++){
sound.setOnClickListener(this);
im_a.setOnClickListener(this);
im_b.setOnClickListener(this);
im_c.setOnClickListener(this);
im_d.setOnClickListener(this);
im_e.setOnClickListener(this);
im_f.setOnClickListener(this);
}
// setContentView(R.layout.success_exam1);
//
// im_success = (ImageButton) findViewById(R.id.im_button_success);
// im_success.setOnClickListener(this);
//
//
// setContentView(R.layout.fail_exam1);
// im_fail=(ImageButton) findViewById(R.id.im_button_fail);
// im_fail.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.exam_class_1, menu);
return true;
}
public void onClick(View v) {
// TODO Auto-generated method stub
int opt= v.getId();
if(opt==R.id.imageButton_sound){
if(ks==0){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==1){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==2){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==3){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==4){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
else if(ks==5){
letter_sound = MediaPlayer.create(this,R.raw.a);
letter_sound.start();
ks++;
}
}
else{
if((opt == R.id.imageButton_a && ks==1)||(opt == R.id.imageButton_b && ks==2)||
(opt == R.id.imageButton_c && ks==3)||(opt == R.id.imageButton_d && ks==4)||
(opt == R.id.imageButton_e && ks==5)||(opt == R.id.imageButton_f && ks==6)){
setContentView(R.layout.success_exam1);
joy_sound = MediaPlayer.create(this,R.raw.joy);
joy_sound.start();
}
else{
setContentView(R.layout.fail_exam1);
wrong_sound = MediaPlayer.create(this,R.raw.wrong);
wrong_sound.start();
}
}
}
public void click_success_fail(View v){
setContentView(R.layout.activity_exam_class_1);
}
}
activity_exam_class_1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageButton
android:id="@+id/imageButton_c"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:contentDescription="@null"
android:src="@drawable/exam_c" />
<ImageButton
android:id="@+id/imageButton_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:contentDescription="@null"
android:src="@drawable/exam_a" />
<ImageButton
android:id="@+id/imageButton_d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="@null"
android:src="@drawable/exam_d" />
<ImageButton
android:id="@+id/imageButton_sound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton_f"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:contentDescription="@null"
android:src="@drawable/audio_icon"
/>
<ImageButton
android:id="@+id/imageButton_f"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton_e"
android:layout_alignLeft="@+id/imageButton_d"
android:contentDescription="@null"
android:src="@drawable/exam_f" />
<ImageButton
android:id="@+id/imageButton_e"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton_a"
android:layout_alignLeft="@+id/imageButton_sound"
android:layout_marginLeft="44dp"
android:contentDescription="@null"
android:src="@drawable/exam_e" />
<ImageButton
android:id="@+id/imageButton_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageButton_c"
android:layout_alignRight="@+id/imageButton_sound"
android:layout_marginRight="47dp"
android:contentDescription="@null"
android:src="@drawable/exam_b" />
</RelativeLayout>
success_exam.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/tick1">
<ImageButton
android:id="@+id/im_button_success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/next"
android:contentDescription="@null"
android:onClick="click_success_fail"
/>
</RelativeLayout>
fail_exam.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/wrong1" >
<ImageButton
android:id="@+id/im_button_fail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@drawable/try_again"
android:contentDescription="@null"
android:onClick="click_success_fail"
/>
</RelativeLayout>
我試過這個,但沒有工作。可以請告訴我應該在哪一行重新初始化? – STM 2014-10-02 14:57:44
嘗試在上述每個「其他」語句後重新初始化兩行。 – said 2014-10-02 15:54:49