Using Switch Statement to Handle Button Clicks

后端 未结 7 849
梦毁少年i
梦毁少年i 2020-12-05 03:15

I\'m trying to wrap my head around Views, Listeners etc. I have an Activity with 2 Buttons: buttonplay and buttonstop. My problem is I can\'t wrap my head around the Views

相关标签:
7条回答
  • 2020-12-05 03:44

    Just change the class (I suppose it's the main Activity) to implement View.OnClickListener and on the onClick method put the general onClick actions you want to put:

    public class MediaPlayer extends Activity implements OnClickListener {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Button b1 = (Button) findViewById(R.id.buttonplay);       
        b1.setOnClickListener(this);
        //{YOUR APP}
    }
    
    
    @Override
    public void onClick(View v) {
        // Perform action on click
        switch(v.getId()) {
        case R.id.buttonplay:
            //Play voicefile
            MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();
            break;
        case R.id.buttonstop:
            //Stop MediaPlayer
            MediaPlayer.create(getBaseContext(), R.raw.voicefile).stop();
            break;
        }
    
    }}
    

    I took it from this video: http://www.youtube.com/watch?v=rm-hNlTD1H0 . It's good for starters.

    0 讨论(0)
  • 2020-12-05 03:53

    One way of achieving this is to make your class implement OnClickListener and then add it to your buttons like this:

    Example:

    //make your class implement OnClickListener
    public class MyClass implements OnClickListener{
    
            ...
    
            //Create your buttons and set their onClickListener to "this"
            Button b1 = (Button) findViewById(R.id.buttonplay);   
            b1.setOnClickListener(this);
    
            Button b2 = (Button) findViewById(R.id.buttonstop);   
            b2.setOnClickListener(this);
    
            ...
    
            //implement the onClick method here
            public void onClick(View v) {
               // Perform action on click
              switch(v.getId()) {
                case R.id.buttonplay:
                  //Play voicefile
                  MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();
                  break;
                case R.id.buttonstop:
                  //Stop MediaPlayer
                  MediaPlayer.create(getBaseContext(), R.raw.voicefile).stop();
                  break;
              }
           }
    }
    

    For more information see Android Developers > Handling UI Events.

    0 讨论(0)
  • 2020-12-05 03:55

    I use Butterknife with switch-case to handle this kind of cases:

        @OnClick({R.id.button_bireysel, R.id.button_kurumsal})
            public void onViewClicked(View view) {
                switch (view.getId()) {
    
    
                    case R.id.button_bireysel:
     //Do something
    
                        break;
                    case R.id.button_kurumsal:
    
         //Do something
    
                        break;
    
                }
            }
    

    But the thing is there is no default case and switch statement falls through

    0 讨论(0)
  • 2020-12-05 03:56

    I have found that the simplest way to do this is to set onClick for each button in the xml

    <Button
    android:id="@+id/vrHelp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_menu_help"
    android:onClick="helpB" />
    

    and then you can do a switch case like this

      public void helpB(View v) {
        Button clickedButton = (Button) v;
        switch (clickedButton.getId()) {
          case R.id.vrHelp:
            dosomething...
            break;
    
          case R.id.coHelp:
            dosomething...
            break;
    
          case R.id.ksHelp:
            dosomething...
            break;
    
          case R.id.uHelp:
            dosomething...
            break;
    
          case R.id.pHelp:
            dosomething...
            break;
        }
      }
    
    0 讨论(0)
  • 2020-12-05 04:02

    One mistake what i did was not including "implements OnClickListener" in the main class declaration. This is a sample code to clearly illustrate the use of switch case during on click. The code changes background colour as per the button pressed. Hope this helps.

    public class MainActivity extends Activity  implements OnClickListener{
    
    
    TextView displayText;
    Button cred, cblack, cgreen, cyellow, cwhite;
    LinearLayout buttonLayout;
    
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        cred = (Button) findViewById(R.id.bred);
        cblack = (Button) findViewById(R.id.bblack);
        cgreen = (Button) findViewById(R.id.bgreen);
        cyellow = (Button) findViewById(R.id.byellow);
        cwhite = (Button) findViewById(R.id.bwhite);
        displayText = (TextView) findViewById(R.id.tvdisplay);
        buttonLayout = (LinearLayout) findViewById(R.id.llbuttons);
    
        cred.setOnClickListener(this);
        cblack.setOnClickListener(this);
        cgreen.setOnClickListener(this);
        cyellow.setOnClickListener(this);
        cwhite.setOnClickListener(this);
    }
    
    @Override
    protected void onClick(View V){
        int id=V.getId();
        switch(id){
        case R.id.bred:
            displayText.setBackgroundColor(Color.rgb(255, 0, 0));
            vanishLayout();
            break;
        case R.id.bblack:
            displayText.setBackgroundColor(Color.rgb(0, 0, 0));
            vanishLayout();
            break;
        case R.id.byellow:
            displayText.setBackgroundColor(Color.rgb(255, 255, 0));
            vanishLayout();
            break;
        case R.id.bgreen:
            displayText.setBackgroundColor(Color.rgb(0, 255, 0));
            vanishLayout();
            break;
        case R.id.bwhite:
            displayText.setBackgroundColor(Color.rgb(255, 255, 255));
            vanishLayout();
            break;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 04:03
        XML CODE FOR TWO BUTTONS  
         <Button
                android:id="@+id/btn_save"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="SAVE"
                android:onClick="process"
                />
            <Button
                android:id="@+id/btn_show"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="SHOW"
                android:onClick="process"/> 
    
      Java Code
     <pre> public void process(View view) {
                switch (view.getId()){
                    case R.id.btn_save:
                      //add your own code
                        break;
                    case R.id.btn_show:
                       //add your own code
                        break;
                }</pre>
    
    0 讨论(0)
提交回复
热议问题