Android 一个 OnClick 方法可用于多个按钮?

发布于 2024-12-12 00:03:44 字数 211 浏览 0 评论 0原文

我在android上开始了一些程序, 我在一个 Activity 中有 3 个按钮。

我看到一些示例代码将相同的 OnClick 事件分配给所有按钮(即使它们执行完全不同的操作),并且在方法 Switch(id) case case 中。 ..

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

谢谢。

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

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...

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

停顿的约定 2024-12-19 00:03:44

使用这种方式:

@Override
public void onCreate(Bundle savedInstanceState) {
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
}

private OnClickListener onClickListener = new OnClickListener() {
     @Override
     public void onClick(View v) {
         switch(v.getId()){
             case R.id.button1:
                  //DO something
             break;
             case R.id.button2:
                  //DO something
             break;
             case R.id.button3:
                  //DO something
             break;
         }

   }
};

Use this way:

@Override
public void onCreate(Bundle savedInstanceState) {
        button1.setOnClickListener(onClickListener);
        button2.setOnClickListener(onClickListener);
        button3.setOnClickListener(onClickListener);
}

private OnClickListener onClickListener = new OnClickListener() {
     @Override
     public void onClick(View v) {
         switch(v.getId()){
             case R.id.button1:
                  //DO something
             break;
             case R.id.button2:
                  //DO something
             break;
             case R.id.button3:
                  //DO something
             break;
         }

   }
};
请远离我 2024-12-19 00:03:44

如果你想减少代码行,那么使用 View 的 OnClick() 和 switch 语句,如果你想单独处理所有点击(以便于理解和维护代码),那么使用单独的所有 button 的 onClick ().

更新:

如果您在 Activity 布局 xml 文件中声明了按钮,请为所有按钮编写具有相同方法名称的属性 android:onClick=""按钮并在您的活动中实现该方法。现在您拥有一种适用于所有按钮的方法,并在该方法中通过 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>

现在在您的 Activity 中实现 buttonOnClick

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

或者您可以为 Activity 中动态添加的按钮应用相同的 switch case,
就像代替 buttonOnClick 一样,您必须使用实现的 View 的 OnClickListerner 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().

Update:

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.

Example:

<?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>

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

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.

遥远的绿洲 2024-12-19 00:03:44
this.btnAddFriedtoFacebook = (Button) this.findViewById(R.id.btnAddFriedtoFacebook);
this.btnAddFriedtoFacebook.setOnClickListener(this.backButtonClickListener);

public OnClickListener backButtonClickListener = new OnClickListener()
{
public void onClick(final View view)
{
   if (view == MatchInfoActivity.this.btnBack)
    {
        MatchInfoActivity.this.finish();
    }
    if( view == MatchInfoActivity.this.btnAddFried){
       Intent i = new Intent(Intent.ACTION_VIEW);
       MatchInfoActivity.this.startActivity(i);
    }
    if( view == MatchInfoActivity.this.btnAddBuddy){
       Intent i = new Intent(Intent.ACTION_VIEW);
       MatchInfoActivity.this.startActivity(i);
    }
}
};

这是好方法。

this.btnAddFriedtoFacebook = (Button) this.findViewById(R.id.btnAddFriedtoFacebook);
this.btnAddFriedtoFacebook.setOnClickListener(this.backButtonClickListener);

public OnClickListener backButtonClickListener = new OnClickListener()
{
public void onClick(final View view)
{
   if (view == MatchInfoActivity.this.btnBack)
    {
        MatchInfoActivity.this.finish();
    }
    if( view == MatchInfoActivity.this.btnAddFried){
       Intent i = new Intent(Intent.ACTION_VIEW);
       MatchInfoActivity.this.startActivity(i);
    }
    if( view == MatchInfoActivity.this.btnAddBuddy){
       Intent i = new Intent(Intent.ACTION_VIEW);
       MatchInfoActivity.this.startActivity(i);
    }
}
};

Here is the good way.

要走干脆点 2024-12-19 00:03:44

我认为在 xml(布局)中注册 onClick 是更好的方法。

编辑:

找到相关线程:

  1. 在android中定义按钮事件的最佳实践
  2. 处理 UI 事件的最佳实践

I think registering onClick in xml (layout) is better approach.

EDIT:

Found related threads :

  1. Best practice for defining button events in android
  2. best practices for handling UI events
初吻给了烟 2024-12-19 00:03:44

在XML布局中注册onClick事件,然后在代码中处理它。我就是这样做的:

<Button
android:id="@+id/btplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="onBtnClicked">

.class 中的方法

public void onBtnClicked(View v) {
        switch (v.getId()) {
        case R.id.btplus:
            Toast.makeText(getApplicationContext(), "Plus is clicked" + "+", Toast.LENGTH_SHORT).show(); 
            break;
        case R.id.btminu:
            Toast.makeText(getApplicationContext(),"Minus is clicked" + "-", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
    }

Registered onClick event in the XML layout and then handle it in the code. This is how I would do it:

<Button
android:id="@+id/btplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="onBtnClicked">

Method in .class

public void onBtnClicked(View v) {
        switch (v.getId()) {
        case R.id.btplus:
            Toast.makeText(getApplicationContext(), "Plus is clicked" + "+", Toast.LENGTH_SHORT).show(); 
            break;
        case R.id.btminu:
            Toast.makeText(getApplicationContext(),"Minus is clicked" + "-", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
        }
    }
糖果控 2024-12-19 00:03:44

对@Nguyen 答案的补充很少。

findViewById(R.id.buttonOne).setOnClickListener(buttonClickListener);
....    ....    ....    ....
findViewById(R.id.buttonN).setOnClickListener(buttonClickListener);

private View.OnClickListener buttonClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonOne:
                // do something
                break;
            ....    ....    ....    ....
            case R.id.buttonN:
                // do something
                break;
        }
    }
};

如果您不想初始化按钮变量,但想要跟踪按钮单击事件,这可能很有用。谢谢!

Little addition to @Nguyen answer.

findViewById(R.id.buttonOne).setOnClickListener(buttonClickListener);
....    ....    ....    ....
findViewById(R.id.buttonN).setOnClickListener(buttonClickListener);

private View.OnClickListener buttonClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.buttonOne:
                // do something
                break;
            ....    ....    ....    ....
            case R.id.buttonN:
                // do something
                break;
        }
    }
};

This could be useful, if you don't want to initialize the button variable, but want to track button click event. Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文