如何捕捉一个按钮单击事件? [英] How to catch a click event on a button?

查看:298
本文介绍了如何捕捉一个按钮单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿大家。 .NET开发人员在这里才刚刚开始使用Eclipse和Android。

Hey everyone. .NET Developer here just getting started with Eclipse and Android.

有人能告诉我在可能的最简单的方法,用code的绝对最少的线路,如何做一个按钮被点击的时候?

Can someone show me in the simplest way possible, with the absolute fewest lines of code, how to DO something when a button is clicked?

我的按钮具有ID 按钮1 ,我只是想看看这里的/怎么写的onClick()处理程序

My button has id button1 and I just want to see where/how to write the onClick() handler.

所以我们可以说我有 imageview1 设置为不可见。我将如何使其可见单击该按钮时?

So let's say I have imageview1 set to invisible. How would I make it visible when the button is clicked?

编辑:

谢谢大家,但因为不是你的例子单一工作对我来说,我会试试这个:有人可以请张贴整个code,使这项工作?不只是方法,监守当我尝试使用任何你的方法,我得到所有的地方错误,所以很明显别的东西丢失。我需要看到的一切,所有的进口开始。谢谢你。

Thanks everyone, but since not a single one of your examples work for me, I'll try this: Can someone please post the ENTIRE code to make this work? Not just the method, becuase when I try to use ANY of your methods I get errors all over the place so obviously something else is missing. I need to see everything, beginning with all the imports. Thank you.

推荐答案

/src/com/example/MyClass.java

/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

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

这篇关于如何捕捉一个按钮单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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