如何捕获按钮上的单击事件?

时间:2011-05-03 13:04:21

标签: android

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

有人可以用最简单的方式向我展示,使用绝对最少的代码行,在点击按钮时如何做某事?

我的按钮标识为button1,我只是想知道在哪里/如何编写onClick()处理程序。

所以我要说imageview1设置为隐身。单击按钮后如何使其可见?

编辑:

谢谢大家,但由于您的示例中没有一个适用于我,我会尝试这样做:有人可以发布整个代码来完成这项工作吗?不仅仅是方法,因为当我尝试使用你的任何方法时,我会在整个地方出现错误,所以显然缺少其他东西。从所有进口开始,我需要看到一切。

7 个答案:

答案 0 :(得分:38)

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

答案 1 :(得分:27)

绝对最好的方法:让您的活动实施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" />

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

答案 2 :(得分:7)

只需声明一个方法,例如:如果你的按钮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);

答案 3 :(得分:5)

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

一个活动需要实现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());
   }
}

答案 4 :(得分:4)

取自: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);
    ...
}

答案 5 :(得分:0)

您可以在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);
        }
    }
}

答案 6 :(得分:0)

将onCreate更改为

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

对我来说,此更新有效

相关问题