button的OnClickListener的三种实现方法

原文连接:http://blog.csdn.net/huiwolf2008/article/details/7988602

onclick事件的定义方法,分为三种,分别为在xml中进行指定方法;在Actitivy中new出一个OnClickListenner();实现OnClickListener接口三种方式。

代码分别如下:

1. xml指定onclick事件,这种方式比较适用于指定的button,能使Java代码相对简化一些:

xml文件中:

<Button
        android:layout_width="300px"
        android:layout_height="130px"
        android:text="xml"
        android:id="@+id/button1"
        android:onClick="clickbutton1"
        android:layout_below="@+id/t114extView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="31dp" />

在Activity中定义方法:
public void clickbutton1(View view)
    {
        text=(TextView)findViewById(R.id.t114extView);
        text.setText("通过xml指定");
    }

2.在onCreate方法中为button指定绑定操作。xml里如果同时指定了事件的执行方法,优先执行xml中的内容。
Button btn = (Button) findViewById(R.id.button2);
        btn.setOnClickListener(
                new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        text=(TextView)findViewById(R.id.t114extView);
                        text.setText("通过oncreate指定");
                    }
                }
                              );

3.实现onclicklistener接口,一次解决所有的onclick问题。  
(1)首先在onCreate方法中加入以下代码:
Button btn3;
        btn3=(Button)findViewById(R.id.button3);
        btn3.setOnClickListener(handler);
(2)然后在acticity中加入以下代码:
View.OnClickListener handler = new View.OnClickListener()
    {
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.button3:
                    text = (TextView) findViewById(R.id.t114extView);
                    text.setText("通过onclicklistener指定");
                    break;
            }
        }
    };

这三种方法都能实现点击事件的处理,可以根据实际的使用环境,使用合适的方法。

下面整合到一个工程中:
(1)xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="kun.kun.MainActivity">



    <TextView
        android:layout_width="800px"
        android:layout_height="70px"
        android:text="Hello World!"
        android:id="@+id/t114extView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="300px"
        android:layout_height="130px"
        android:text="xml"
        android:id="@+id/button1"
        android:onClick="clickbutton1"
        android:layout_below="@+id/t114extView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="31dp" />

    <Button
        android:layout_width="300px"
        android:layout_height="130px"
        android:text="onCreate"
        android:id="@+id/button2"
        android:layout_below="@+id/button1"
        android:layout_alignStart="@+id/button1"
        android:layout_marginTop="70dp" />

    <Button
        android:layout_width="300px"
        android:layout_height="130px"
        android:text="listen"
        android:id="@+id/button3"
        android:layout_below="@+id/button2"
        android:layout_alignStart="@+id/button2"
        android:layout_marginTop="65dp" />

</RelativeLayout>

(2)java文件:
package kun.kun;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.button2);
        btn.setOnClickListener(
                new View.OnClickListener()
                {
                    public void onClick(View v)
                    {
                        text=(TextView)findViewById(R.id.t114extView);
                        text.setText("通过oncreate指定");
                    }
                }
                              );
        Button btn3;
        btn3=(Button)findViewById(R.id.button3);
        btn3.setOnClickListener(handler);

    }
    public void clickbutton1(View view)
    {
        text=(TextView)findViewById(R.id.t114extView);
        text.setText("通过xml指定");
    }

    View.OnClickListener handler = new View.OnClickListener()
    {
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.button3:
                    text = (TextView) findViewById(R.id.t114extView);
                    text.setText("通过onclicklistener指定");
                    break;
            }
        }
    };
}

GIF: 链接






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值