的Andr​​oid 1 OnClick方法用于多个按钮? [英] Android one OnClick method for multiple buttons?

查看:131
本文介绍了的Andr​​oid 1 OnClick方法用于多个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始计划点点在Android中, 我有一个活动的3个按键。

I started program little bit in android, I have 3 buttons in a single activity.

我看到一些例如codeS,同样的的OnClick 事件指派给所有的按钮(即使他们的表现完全不同的动作),在方法开关(ID)审理的案件的情况下...

I saw some example codes that assign the same OnClick event to all the buttons (even if they perform completely different action) and in the method Switch(id) case case case...

什么是更好的方法?一个的onClick 方法和开关还是有很多方法,每一个按钮?

What is the better approach? one onClick method and switching or a lot of methods, one for each button?

感谢。

推荐答案

如果你想减少编码线,然后使用查看的OnClick()与switch语句如果你要单独处理所有点击(为便于理解和维护code),然后使用单独的所有按钮的onClick()。

If you want to reduce the coding lines then use View's OnClick() with switch statement and if you want to handle separately all click (for easily understanding and maintaining code) then use separate all button's onClick().

更新:

如果你声明的按钮在你的活动布局的xml文件,不是写属性安卓的onClick =用同样的方法名进行的所有按钮和实施该方法的活动。现在你有一个方法,所有按钮和该方法区分按键与标识。

If you have declared Buttons in your Activity layout xml file, than write attribute android:onClick="" with same method name for all buttons and implement that method in your activity. Now you have one method for all buttons and in that method differentiate buttons with id.

示例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <Button android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 1" />
    <Button android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 2" />
    <Button android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:onClick="buttonOnClick"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button 3" />
</LinearLayout>

现在在你的活动实施 buttonOnClick 等,

Now in your Activity implement buttonOnClick like,

public void buttonOnClick(View view)
{
 switch(view.getId())
 {
  case R.id.button1:
  // Code for button 1 click
  break;

  case R.id.button2:
  // Code for button 2 click
  break;

  case R.id.button3:
  // Code for button 3 click
  break;
 }
}

或者你也可以使用同一台交换机的情况下为您的活动动态添加按钮, 像,而不是 buttonOnClick ,你必须使用实现视图的OnClickListerner的 的onClick

Or you can apply same switch case for dynamically added buttons in your activity, like instead of buttonOnClick you have to use implemented View's OnClickListerner's onClick.

这篇关于的Andr​​oid 1 OnClick方法用于多个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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