使用switch语句来处理按钮点击声 [英] Using Switch Statement to Handle Button Clicks

查看:153
本文介绍了使用switch语句来处理按钮点击声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想环绕查看我的头,监听器等,我有一个活动,2个按钮:buttonplay和buttonstop。我的问题是我不能环绕视图和听众完全足以产生一个工作switch语句我的头。

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.

例如,我想创建一个侦听器,并以某种方式使用它来确定被点击的按钮。然后以某种方式使用点击我的switch语句按钮的ID,但是,一切都我在网上找似乎使用单独的侦听器为每一个按钮,然后以某种方式使用视图作为参数传递给switch语句。

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.

我意识到低于code是不正确的,但我在寻找什么样的变化,我需要完成以上。

I realize the code below is not correct, but am looking for what changes I would need to accomplish the above.

我想控制视按钮被点击其上的MediaPlayer。我有:

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

最后,我想最straighforward方式上任何按钮被点击进行切换。我相信我的困惑的很大一部分来自onClickListeners和意见是在这种情况下使用的方式茎。

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.

}

推荐答案

实现这一目标是使你的类实现OnClickListener,然后将其添加到您的按钮,像这样的一种方式:

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

例如:

//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;
          }
       }
}

有关详细信息,请参见 Android开发者>处理UI事件的。

For more information see Android Developers > Handling UI Events.

这篇关于使用switch语句来处理按钮点击声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆