Button与ImageButton的点击监听事件-onClick

版权声明:禁止转载,谢谢合作。 https://blog.csdn.net/oschina_41423692/article/details/82944173

Button与ImageButton自身都有一个onClick点击事件,通过自身的.setOnClickListener(OnClickListener)的方法添加点击事件。

所有控件都有一个OnClick事件。通过点击事件的监听可以实现点击后发生什么动作。

监听事件实现的几种方法:

1.匿名内部类

2.独立类的实现

3.接口的形式实现 

一、匿名内部类的实现Button监听事件

1.初始化控件

2.通过findViewById返回一个view对象,然后转换成Button 赋值给初始化的控件

3.设置Button的监听器,通过监听器实现点Button要操作的事情

XML代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Listener">


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:id="@+id/button1"/>

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton1"
        android:src="@drawable/ic_launcher"/>
</RelativeLayout>

业务逻辑代码:

package com.xuzone.appbasic.listener;

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


public class Listener extends Activity {

    //初始化登陆按钮
    private Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listener);
        /*
        * 1.初始化所需要的控件
        *  通过findViewById返回一个view对象,然后转换成Button
        * 2.设置Button的监听器,通过监听器实现点Button要操作的事情
        *
        * */

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

        /*
        * 1.监听事件通过内部匿名类实现
        */
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在当前方法onClick中监听
                //Tost为监听后的操作
                Toast.makeText(Listener.this,"点击成功",Toast.LENGTH_SHORT).show();

            }
        });
    }
}

二、独立类实现按钮点击的监听

外部独立类可以把多个监听事件相同的的内容集中到外部类中饭,然后在各各子类可以通过super进行调用外部类的内容,同时可以在自己的类中添加自己按钮独有的内容或操作。

XML代码:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:id="@+id/button2"
        android:layout_below="@+id/button1"
        android:layout_alignParentStart="true"
        android:layout_marginTop="70dp" />

</RelativeLayout>

业务逻辑代码:

package com.xuzone.appbasic.listener2;

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


public class Listener2 extends Activity {

    private Button btn1;
    private Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listener2);

        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);

//        btn1.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//
//            }
//        });


        /*
        *   点击外部类的写法和作用
        */
                                    //实现外部类
        btn1.setOnClickListener(new MyOnClickListener(){
            @Override
            public void onClick(View v) {
            //调用父类的onClick
            super.onClick(v);
            Toast.makeText(Listener2.this,"btn1要执行的逻辑",Toast.LENGTH_SHORT).show();

            }
        });

        btn2.setOnClickListener(new MyOnClickListener(){
            @Override
            public void onClick(View v) {
            //调用父类的onClick
            super.onClick(v);
            Toast.makeText(Listener2.this,"btn2要执行的逻辑",Toast.LENGTH_SHORT).show();

            }
        });
    }
}

//外部类,其中OnClickListener是一个接口
class MyOnClickListener implements OnClickListener{
    @Override
    public void onClick(View v) {
        //让所有使用外部类的点击事件的外部按钮,改变button的按钮颜色
        v.setAlpha(0.5f);

    }
}

三、通过接口的形式实现监听

让Activity类实现接口OnClickListener的监听事件方法,然后给相应的按钮绑定上监听事件。

XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Listener3">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="按钮1" />
</RelativeLayout>

业务逻辑代码:

package com.xuzone.appbasic.listener3;

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

//OnClickListener是接口
public class Listener3 extends Activity implements OnClickListener {

    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listener3);
        //获取按钮
        btn = (Button)findViewById(R.id.button);
        //给按钮绑定监听事件,
        btn.setOnClickListener(this);
    }

    //通过实现接口的监听事件onClick,实现监听事件
    @Override
    public void onClick(View v){
        Toast.makeText(Listener3.this, "通过接口实现点击监听", Toast.LENGTH_SHORT).show();
    }

}

不同的功能需求选择不同的实现方式。

猜你喜欢

转载自blog.csdn.net/oschina_41423692/article/details/82944173