五种Button监听方式总结
来源:互联网 发布:centos如何安装ssh 编辑:程序博客网 时间:2024/06/05 02:26
本类监听 – 使Activity继承View.OnClickListener接口,实现其onClick方法,然后在setOnClickListener函数里面直接传this
外部类监听 –新建一个外部类,继承View.OnClickListener接口,实现其onClick方法,然后在setOnClickListener函数里面直接new一个新建的类
内部类监听 – 新建一个内部类,继承View.OnClickListener接口,实现其onClick方法,然后在setOnClickListener函数里面直接new一个新建的类
匿名监听 – 直接在set里new一个OnClickListener并实现onClick方法
布局监听 – 布局里onClick属性直接赋值给getInfo方法
Activity.java
package com.jredu.ch01;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.Toast;public class Main3Activity extends AppCompatActivity implements View.OnClickListener{ private Button bt1,bt2,bt3,bt5; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); bt1=(Button)findViewById(R.id.bt1); //内部类监听 bt2=(Button)findViewById(R.id.bt2); //匿名类监听 bt3=(Button)findViewById(R.id.bt3); //本类监听 bt5=(Button)findViewById(R.id.bt5); //外部类监听 bt1.setOnClickListener(new Bt1Click()); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getBaseContext(), "匿名类监听", Toast.LENGTH_SHORT).show(); } }); bt3.setOnClickListener(this); bt5.setOnClickListener(new MyOnClickListener()); } @Override public void onClick(View v) { Toast.makeText(getBaseContext(),"本类监听",Toast.LENGTH_SHORT).show(); } public class Bt1Click implements View.OnClickListener{ @Override public void onClick(View v) { Toast.makeText(getBaseContext(), "内部类监听", Toast.LENGTH_SHORT).show(); } } public void getInfo(View view){ Toast.makeText(getBaseContext(),"布局监听",Toast.LENGTH_SHORT).show(); }}
Activity.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: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="com.jredu.ch01.Main3Activity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内部" android:id="@+id/bt1" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="匿名" android:id="@+id/bt2" android:layout_below="@+id/bt1" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="本类" android:id="@+id/bt3" android:layout_below="@+id/bt2" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="布局" android:onClick="getInfo" android:id="@+id/bt4" android:layout_below="@+id/bt3" android:layout_alignEnd="@+id/bt1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="外部类" android:id="@+id/bt5" android:layout_below="@+id/bt4" android:layout_alignParentStart="true" /></RelativeLayout>
1 0
- 五种Button监听方式总结
- android Button 监听的几种方式
- android Button 监听的几种方式
- android Button 监听的几种方式
- android Button 监听的几种方式
- android Button 监听的几种方式
- android Button 监听的几种方式
- android Button 监听的几种方式
- Button的四种监听方式
- android Button 监听的几种方式
- button按钮的四种监听方式
- 实现button的监听,3种方式
- Button设置监听的三种方式
- 【Android】Button实现监听的三种方式
- android Button 按钮监听的几种方式
- 一个demo弄懂Button控件的四种监听方式
- Andriod 中button监听事件的三种方式
- Android -- Button(按钮)的几种监听方式
- C语言单链表的创建,插入,删除,逆致
- 深入理解 Java 虚拟机-Java 语法糖
- 面试中遇到的SQL题目
- 50个经典常用的sql语句
- Longest Substring with At Most K Distinct Characters
- 五种Button监听方式总结
- npm介绍
- HttpUtils
- JS编写冒泡排序法和二分查找
- Bear and Polynomials 639 C
- 设计模式:组合模式(Composite)
- 先根递归建立二叉树(顺序表示法)
- yii2分页
- Ubuntu安装Torch