开关和案例使按钮可见

时间:2013-10-16 07:41:24

标签: android button switch-statement case

这是带有开关案例代码的源代码。但大胆的部分没有奏效。按下按钮D,按钮0和按钮G后,我尝试使“下一个”可见。仅供参考,“下一步”是一个按钮。所以它使用三个条件来制作一个命令。它是如何工作的?我把那些代码放错了地方吗?

 package com.example.fun;

    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.util.Log;
    import android.view.KeyEvent;
    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;


        @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); 
                  }
                  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--;
                      Log.i("Current Life Value is",""+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;
                    }

                  }

              }

    };
    **private boolean clickedBtnD;
    private boolean clickedBtnO;
    private boolean clickedBtnG;

    public void onClick(View v) {
        final int id = v.getId();
        switch(id) {
            case R.id.buttond:
                clickedBtnD = true;
                txtD.setVisibility(View.VISIBLE);
                break;
            case R.id.buttono:
                clickedBtnO = true;
                txtO.setVisibility(View.VISIBLE);
                break;
            case R.id.buttong:
                clickedBtnG = true;
                txtG.setVisibility(View.VISIBLE);
                break;
            case R.id.buttonnext:
                if(clickedBtnD && clickedBtnO && clickedBtnG) {
                    next.setVisibility(View.VISIBLE);
                }
                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;
        }
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                 i = new Intent(DogActivity.this, LevelActivity.class);
                    startActivity(i);

            }   
            return super.onKeyDown(keyCode, event);
            }
    }

1 个答案:

答案 0 :(得分:0)

你缺少在你的onclick方法之前从你的Activity和@Override实现onClicklistenor,比如在代码中只有一个按钮就在这里..

试试这段代码

package com.example.browsebutton;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button)findViewById(R.id.button1);


        btn.setOnClickListener(this);

    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        int id=v.getId();
         switch(id) {
         case R.id.button1:
             //CODE
             break;
    }

}
}