java 按钮上的多次单击侦听器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45581530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:46:45  来源:igfitidea点击:

Multiple click listeners on buttons

javaandroidbuttononclickkotlin

提问by Anand Rajput

I want to know how to add multiple click events to buttons defined in XML, as previously, in Java, we implemented View.onClickListenerinterface and did the rest of the work in onClickmethod.
Example:

我想知道如何将多个单击事件添加到 XML 中定义的按钮,就像以前一样,在 Java 中,我们实现了View.onClickListener接口并在onClick方法中完成了其余的工作。
例子:

@Override
public void onClick(View v) {

switch (v.getId()) {

    case R.id.oneButton:
        // do your code
        break;

    case R.id.twoButton:
        // do your code
        break;

    case R.id.threeButton:
        // do your code
        break;

    default:
        break;
    }

}

I'm making a basic calculator app with the new Kotlin but it seems Kotlin has no such provisions, instead my code looks too long and verbose, as I'm attaching events to all buttons individually.
Can someone tell me how to do the same way in Kotlin? Thanks

我正在使用新的 Kotlin 制作一个基本的计算器应用程序,但似乎 Kotlin 没有这样的规定,相反,我的代码看起来太长而且冗长,因为我将事件单独附加到所有按钮。
有人能告诉我如何在 Kotlin 中做同样的事情吗?谢谢

采纳答案by Rahul

First of all implement OnClickListenerin your Activity, like

首先OnClickListener在你的Activity, 像

class MainActivity : Activity , OnClickListener

then override its implementation like

然后像覆盖它的实现一样

func onClick(v:View) {  
   //use when here like
   case R.id.youview -> {
   // do your work on click of view
    }

Don't forgot to set clicklistener on your View.

不要忘记在您的View.

  yourView.setOnClickListener(this)

Or for better understanding go step by step -

或者为了更好地理解一步一步 -

  1. Implement OnClickListenerin your Activity.

  2. Compiler will ask you to implement overrided methods. Implement those.

  3. Copy paste your java code which you wrote inside onClickmethod, that can be converted by kotlin itself or write down whenconditions.

  1. 实现OnClickListener你的Activity

  2. 编译器会要求你实现重写的方法。实施那些。

  3. 复制粘贴您在onClick方法中编写的java代码,可以由kotlin本身转换或写下when条件。

回答by Sudheesh R

For multiple onClickListeners in kotlin(version:1.1.60), the following helped me. Hope it'll be useful to someone else too.

对于 中的多个 onClickListeners kotlin(version:1.1.60),以下内容对我有帮助。希望它对其他人也有用。

Implement OnClickListenerto activity like:

实施OnClickListener活动,如:

class YourActivity : AppCompatActivity(), View.OnClickListener

set your button in onCreate():

将您的按钮设置为onCreate()

val button = findViewById<Button>(R.id.buttonId)

and assign onclickto the button in onCreate():

并分配onclick给按钮onCreate()

button.setOnClickListener { onClick(button) }

and in the override method of onClick():

并在覆盖方法中onClick()

 override fun onClick(v: View) {
    when (v.id) {
        R.id.buttonId -> { //your code  }
        ..
        ..
        ..
        else -> { //your code  }
   }
 }

回答by rockstar

You can try this following code:

您可以尝试以下代码:

class Testing:AppCompatActivity(), View.OnClickListener {
  private val mButton1:Button
  private val mButton2:Button
  protected fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_testing)
    mButton1 = findViewById(R.id.btn_click) as Button
    mButton2 = findViewById(R.id.btn_click1) as Button
    mButton1.setOnClickListener(this)
    mButton2.setOnClickListener(this)
  }
  fun onClick(view:View) {
    when (view.getId()) {
      R.id.btn_click -> {
        Toast.makeText(this, "button 1", Toast.LENGTH_SHORT).show()
      }
      R.id.btn_click1 -> {
        Toast.makeText(this, "button 2", Toast.LENGTH_SHORT).show()
      }
      else -> {}
    }
  }
}

I hope this is help you.

我希望这对你有帮助。

回答by Kharda

Yes, in Kotlin you can do it like this:

是的,在 Kotlin 中你可以这样做:

view.setOnClickListener(object : View.OnClickListener {
    override fun onClick(v: View?) {
        when(v.id) {
            R.id.imgBack -> {/* do your code */}
            R.id.twoButton -> {/* do your code */}
            R.id.threeButton -> {/* do your code */}
            else -> {/* do your code */}
        }
    }
}

回答by Mengheang

This code worked for me:

这段代码对我有用:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    imgBack.setOnClickListener(this)
    twoButton.setOnClickListener(this)
    threeButton.setOnClickListener(this)
}

override fun onClick(view:View) {
    when (view.id) {
        R.id.imgBack -> {
            Toast.makeText(this, "imgBack", Toast.LENGTH_SHORT).show()
        }
        R.id.twoButton -> {
            Toast.makeText(this, "twoButton", Toast.LENGTH_SHORT).show()
        }
        else -> {}
    }
}

Don't forget implement View.OnClickListenerin your class.

不要忘记View.OnClickListener在你的课堂上实现。

回答by Harshit Trivedi

Try below like this.

试试下面这样。

  public class MainActivity extends Activity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button xyz = (Button) findViewById(R.id.xyz);
        xyz.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) { 
    }}

More detailed information at https://androidacademic.blogspot.in/2016/12/multiple-buttons-onclicklistener-android.html

更多详细信息请访问https://androidacademic.blogspot.in/2016/12/multiple-buttons-onclicklistener-android.html