2014-10-02 37 views
0

在我的程序的一部分在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> 

回答

0

slm;我想你应该在游戏的第一部分结束时重新初始化计数器“int Ks = 0;”。尝试像这样的“RE初始化”:

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)){ 
      Ks=0; 
     setContentView(R.layout.success_exam1); 
    joy_sound = MediaPlayer.create(this,R.raw.joy); 
    joy_sound.start(); 



    } 

    else{ Ks=0; 
     setContentView(R.layout.fail_exam1); 
      wrong_sound = MediaPlayer.create(this,R.raw.wrong); 
      wrong_sound.start(); 


    } 
+0

我试过这个,但没有工作。可以请告诉我应该在哪一行重新初始化? – STM 2014-10-02 14:57:44

+0

尝试在上述每个“其他”语句后重新初始化两行。 – said 2014-10-02 15:54:49

相关问题