【问题标题】:How to catch a click event on a button?如何捕获按钮上的点击事件?
【发布时间】:2011-05-03 13:04:20
【问题描述】:

.NET Developer 在这里刚刚开始使用 Eclipse 和 Android。

谁能以最简单的方式告诉我,用最少的代码行,当一个按钮被点击时如何做某事?

我的按钮 ID 为 button1,我只想看看在哪里/如何编写 onClick() 处理程序。

假设我将imageview1 设置为不可见。单击按钮时如何使其可见?

编辑:

谢谢大家,但由于没有一个示例对我有用,我会试试这个:有人可以发布整个代码来完成这项工作吗?不只是方法,因为当我尝试使用您的任何方法时,我都会在整个地方遇到错误,因此显然还缺少其他东西。我需要查看所有内容,从所有导入开始。

【问题讨论】:

  • 显然,您必须告诉我们您“到处”遇到什么错误...
  • 考虑使用 Android Studio 而不是 Eclipse。

标签: android


【解决方案1】:

/src/com/example/MyClass.java

package com.example

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MyClass extends Activity
{

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    

    Button button = (Button) findViewById(R.id.button1);

    button.setOnClickListener(new OnClickListener()
    {
      public void onClick(View v)
      {
         ImageView iv = (ImageView) findViewById(R.id.imageview1);
         iv.setVisibility(View.VISIBLE);
      }
    });

  }
}

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <Button 
      android:text="Button"
      android:id="@+id/button1"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
    />
    <ImageView 
      android:src="@drawable/image" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:id="@+id/imageview1"
      android:visibility="invisible"
    />
</LinearLayout>

【讨论】:

  • 明白我的意思吗?没有什么工作大声笑。 “新 View.OnClickListener(){} 类型的方法 onClick(View) 必须覆盖超类方法”因为我完全复制了您的文本,所以这个错误的原因是什么?
  • 然后取出@Override。 (以前做过!)
  • 唷,这就像回到来自 .NET 的汇编语言。谢谢。
【解决方案2】:

绝对最好的方法:让你的活动实现View.OnClickListener,然后像这样编写你的onClick方法:

public void onClick(View v) {
    final int id = v.getId();
    switch (id) {
    case R.id.button1:
        // your code for button1 here
        break;
    case R.id.button2:
        // your code for button2 here
        break;
    // even more buttons here
    }
}

然后,在您的 XML 布局文件中,您可以直接使用属性android:onClick 设置点击监听器:

<Button
    android:id="@+id/button1"
    android:onClick="onClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1" />

这是最干净的方法。我今天在我所有的项目中都使用它。

【讨论】:

  • 我个人觉得这很丑:1)如果你重命名方法,Lint 不会警告你,你只会在运行时了解它。 2)该方法必须是公开的才能使其工作。 3) 方法必须在activity中,使用fragment会很烦。
  • @pcans 这取决于您使用它的目的。例如,我发现这个用例非常方便,因为我有一个由 36 个按钮组成的网格要按下,这比其他方式更不“丑陋”。如果您在提到的情况下不使用按钮,这是一种很好的处理方式。
  • 是的,我可能会这样做 onButton1Click(), onButton2Click(), ... 它更简洁一些,并且仍然保持代码库中的所有内容对齐。 (少金字塔码)
【解决方案3】:

只需声明一个方法,例如:如果你的按钮 id 是 button1 那么,

button1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(Context, "Hello", Toast.LENGTH_SHORT).show();
        }
    });

如果你想让 imageview1 可见,那么在那个方法中写:

imageview1.setVisibility(ImageView.VISIBLE);

【讨论】:

  • 用“getApplicationContext()”替换“Context”对我有用:Toast.makeText(getApplicationContext(),“You mashed the button, dude.”, Toast.LENGTH_SHORT).show();
【解决方案4】:

所有答案都基于匿名内部类。我们还有另一种方法可以为按钮以及其他组件添加点击事件。

一个activity需要实现View.OnClickListener接口,我们需要重写onClick函数。与使用匿名类相比,我认为这是最好的方法。

package com.pointerunits.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
   private Button login;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      login = (Button)findViewById(R.id.loginbutton);
      login.setOnClickListener((OnClickListener) this);
      Log.i(DISPLAY_SERVICE, "Activity is created");

   }    

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

      getMenuInflater().inflate(R.menu.main, menu);
      return true;
    }

   @Override
   public void onClick(View v) {
     Log.i(DISPLAY_SERVICE, "Button clicked : " + v.getId());
   }
}

【讨论】:

  • 干净多了。谢谢。
【解决方案5】:

取自:http://developer.android.com/guide/topics/ui/ui-events.html

// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the button is clicked
    }
};

protected void onCreate(Bundle savedValues) {
    ...
    // Capture our button from layout
    Button button = (Button)findViewById(R.id.corky);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(mCorkyListener);
    ...
}

【讨论】:

    【解决方案6】:

    您可以在 Activity 的 onCreate() 方法中执行此操作。例如:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        //Assign the button to a variable
        Button button1 = (Button)findViewById(R.id.button1);
    
        //Assign the ImageView to a final variable, so that it's
        //accessible from an inner class
        ImageView imageView = (ImageView)findViewById(R.id.imageview1);
    
        //Assign it a new OnClickListener
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                imageView.setVisibility(View.VISIBLE);
            }
        }
    }
    

    【讨论】:

      【解决方案7】:

      将您的 onCreate 更改为

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      

      对我来说这个更新有效

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-22
        • 2014-03-21
        • 2013-01-30
        • 2012-02-23
        • 2013-02-02
        • 2016-09-13
        • 1970-01-01
        相关资源
        最近更新 更多