Using Switch Statement to Handle Button Clicks
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 and Listeners completely enough to generate a working switch statement.
For example, I would LIKE to create a SINGLE Listener and somehow use it to determine which button is clicked. Then somehow use the ID of the button clicked in my switch statement, But everything I find online seems to use SEPARATE listeners for every button and then somehow use the View as the argument to the Switch statement.
I realize the code below is not correct, but am looking for what changes I would 开发者_JS百科need to accomplish the above.
I want to control the MediaPlayer depending on which button is clicked. I have:
Button b1 = (Button) findViewById(R.id.buttonplay);
b1.setOnClickListener(new View.OnClickListener()
{
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;
}
}
});
Ultimately I would like the most straighforward way to switch on whatever button is clicked. I believe a big part of my confusion stems from the way onClickListeners and Views are used in this context.
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.
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.
Hi its quite simple to make switch between buttons using switch case:-
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 b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id=v.getId();
switch(id) {
case R.id.button1:
Toast.makeText(getBaseContext(), "btn1", Toast.LENGTH_LONG).show();
//Your Operation
break;
case R.id.button2:
Toast.makeText(getBaseContext(), "btn2", Toast.LENGTH_LONG).show();
//Your Operation
break;
}
}}
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;
}
}
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;
}
}
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
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>
I think one of the things you missed out was that you failed to declare the stop button b2, you only declared and initialized b1.
精彩评论