单击一次后按钮被禁用。我想重复使用它

时间:2014-10-02 14:17:55

标签: android

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

1 个答案:

答案 0 :(得分:0)

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

 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();


     }