0

我有一个 Android 游戏项目,其源代码如下:

package com.example.fun;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;



public class DogActivity extends Activity {
      Button buttonA;
      Button buttonB;
      Button buttonC;
      Button buttonD;
      Button buttonE;
      Button buttonF;
      Button buttonG;
      Button buttonH;
      Button buttonI;
      Button buttonJ;
      Button buttonK;
      Button buttonL;
      Button buttonM;
      Button buttonN;
      Button buttonO;
      Button buttonP;
      Button buttonQ;
      Button buttonR;
      Button buttonS;
      Button buttonT;
      Button buttonU;
      Button buttonV;
      Button buttonX;
      Button buttonW;
      Button buttonY;
      Button buttonZ;
      Button next;
      Intent i;
      TextView txtD;
      TextView txtO;
      TextView txtG;
      int life=3;
      TextView nyaw;
      TextView nyaw2;
      TextView nyaw3;
      TextView kesempatan;
      boolean l1,l2,l3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dog);
        buttonA = (Button)findViewById(R.id.buttonAbout);
        buttonB = (Button)findViewById(R.id.buttonhelp);
        buttonC = (Button)findViewById(R.id.buttonc);
        buttonD = (Button)findViewById(R.id.buttond);
        buttonE = (Button)findViewById(R.id.buttonE);
        buttonF = (Button)findViewById(R.id.buttonf);
        buttonG = (Button)findViewById(R.id.buttong);
        buttonH = (Button)findViewById(R.id.buttonh);
        buttonI = (Button)findViewById(R.id.buttoni);
        buttonJ = (Button)findViewById(R.id.buttonj);
        buttonK = (Button)findViewById(R.id.buttonk);
        buttonL = (Button)findViewById(R.id.buttonl);
        buttonM = (Button)findViewById(R.id.buttonm);
        buttonN = (Button)findViewById(R.id.buttonn);
        buttonO = (Button)findViewById(R.id.buttono);
        buttonP = (Button)findViewById(R.id.buttonp);
        buttonQ = (Button)findViewById(R.id.buttonq);
        buttonR = (Button)findViewById(R.id.buttonr);
        buttonS = (Button)findViewById(R.id.buttons);
        buttonT = (Button)findViewById(R.id.buttont);
        buttonU = (Button)findViewById(R.id.buttonu);
        buttonV = (Button)findViewById(R.id.buttonv);
        buttonW = (Button)findViewById(R.id.buttonw);
        buttonX = (Button)findViewById(R.id.buttonx);
        buttonY = (Button)findViewById(R.id.buttony);
        buttonZ = (Button)findViewById(R.id.buttonz);
        next = (Button)findViewById(R.id.buttonnext);
        txtD = (TextView)findViewById(R.id.textViewD);
        txtO = (TextView)findViewById(R.id.textViewO);
        txtG = (TextView)findViewById(R.id.textViewG);
        nyaw = (TextView)findViewById(R.id.nyawa);
        nyaw2 = (TextView)findViewById(R.id.nyawa2);
        nyaw3 = (TextView)findViewById(R.id.nyawa3);
        buttonA.setOnClickListener(myOnlyhandler);
        buttonB.setOnClickListener(myOnlyhandler);
        buttonC.setOnClickListener(myOnlyhandler);
        buttonD.setOnClickListener(myOnlyhandler);
        buttonE.setOnClickListener(myOnlyhandler);
        buttonF.setOnClickListener(myOnlyhandler);
        buttonG.setOnClickListener(myOnlyhandler);
        buttonH.setOnClickListener(myOnlyhandler);
        buttonI.setOnClickListener(myOnlyhandler);
        buttonJ.setOnClickListener(myOnlyhandler);
        buttonK.setOnClickListener(myOnlyhandler);
        buttonL.setOnClickListener(myOnlyhandler);
        buttonM.setOnClickListener(myOnlyhandler);
        buttonN.setOnClickListener(myOnlyhandler);
        buttonO.setOnClickListener(myOnlyhandler);
        buttonP.setOnClickListener(myOnlyhandler);
        buttonQ.setOnClickListener(myOnlyhandler);
        buttonR.setOnClickListener(myOnlyhandler);
        buttonS.setOnClickListener(myOnlyhandler);
        buttonT.setOnClickListener(myOnlyhandler);
        buttonU.setOnClickListener(myOnlyhandler);
        buttonV.setOnClickListener(myOnlyhandler);
        buttonW.setOnClickListener(myOnlyhandler);
        buttonX.setOnClickListener(myOnlyhandler);
        buttonY.setOnClickListener(myOnlyhandler);
        buttonZ.setOnClickListener(myOnlyhandler);
        next.setOnClickListener(myOnlyhandler);
    }
    View.OnClickListener myOnlyhandler = new View.OnClickListener() {

          public void onClick(View v) {
              if( buttonD.getId() == ((Button)v).getId() ){
                  txtD.setVisibility(View.VISIBLE);
              }
              else if( buttonO.getId() == ((Button)v).getId() ){
                  txtO.setVisibility(View.VISIBLE);
              }
              else if( buttonG.getId() == ((Button)v).getId() ){
                  txtG.setVisibility(View.VISIBLE);
                  i = new Intent(DogActivity.this, HouseActivity.class);
                    startActivity(i);   
              }
              else if( next.getId() == ((Button)v).getId() ){
                  i = new Intent(DogActivity.this, HouseActivity.class);
                    startActivity(i);   
              }
              else{
                  MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.no);
                    mp.start();
                  life--;   
                    **switch (life) {
                    case 2:
                        nyaw.setVisibility(View.INVISIBLE);
                    break;
                    case 1:
                        nyaw2.setVisibility(View.INVISIBLE);
                    break;
                    case 0:
                        nyaw3.setVisibility(View.INVISIBLE);
                        i = new Intent(DogActivity.this, TamatActivity.class);
                        startActivity(i);
                    break;
                }**
              }

          }

};

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

}

但是当我运行它时,开关和案例代码不起作用。是我放错地方还是代码错了?感谢你的回答..

4

1 回答 1

0

您可以检查以下答案以满足您的要求。

这将正常工作,我只是改变了一些句子没有别的。

见下文。

View.OnClickListener myOnlyhandler = new View.OnClickListener() {

      public void onClick(View v) {
          if( R.id.buttond == ((Button)v).getId() ){
              txtD.setVisibility(View.VISIBLE);
          }
          else if( buttonO.getId() == ((Button)v).getId() ){
              txtO.setVisibility(View.VISIBLE);
          }
          else if( R.id.buttono == ((Button)v).getId() ){
              txtG.setVisibility(View.VISIBLE);
              i = new Intent(DogActivity.this, HouseActivity.class);
                startActivity(i);   
          }
          else if( R.id.buttonnext == ((Button)v).getId() ){
              i = new Intent(DogActivity.this, HouseActivity.class);
                startActivity(i);   
          }
          else{
              MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.no);
                mp.start();
              life--;   
                **switch (life) {
                case 2:
                    nyaw.setVisibility(View.INVISIBLE);
                break;
                case 1:
                    nyaw2.setVisibility(View.INVISIBLE);
                break;
                case 0:
                    nyaw3.setVisibility(View.INVISIBLE);
                    i = new Intent(DogActivity.this, TamatActivity.class);
                    startActivity(i);
                break;
            }**
          }

      }
  //You can also use switch case for this.
};
于 2013-10-14T08:57:31.330 回答