package com.example.wang.testapp2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class Main2Activity extends AppCompatActivity { TextView tv_1; Button bt_2; //方法二的内容
Button bt_31; //方法三的内容
Button bt_32; //方法三的内容 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2); tv_1=(TextView)findViewById(R.id.tv_1); //方法二 给bt_2设置监听事件
bt_2=(Button)findViewById(R.id.bt_2); //方法二的内容 bt_2.setOnClickListener(new View.OnClickListener() { //方法二的内容
@Override
public void onClick(View v) { //方法二的内容
tv_1.setText("方法二"); //方法二的内容
} //方法二的内容
}); //方法二的内容 bt_31=(Button)findViewById(R.id.bt_31); //方法三的内容
bt_32=(Button)findViewById(R.id.bt_32); //方法三的内容 bt_OnClickListener bt1=new bt_OnClickListener(); //方法三的内容
bt_31.setOnClickListener(bt1); //方法三的内容
bt_32.setOnClickListener(bt1); //方法三的内容 } //方法一:直接用onclick方法
public void bt_1(View v)
{
tv_1.setText("方法一");
} //方法三
//内部类实现OnClicklistenner接口
class bt_OnClickListener implements View.OnClickListener //方法三的内容
{ @Override
//v 事件源
public void onClick(View v) { //方法三的内容
//转成按钮
Button bt=(Button)v; //方法三的内容
//获得按钮上的文字
String str=bt.getText().toString(); //方法三的内容
//处理事件的业务逻辑 设置显示文字
tv_1.setText(str); //方法三的内容
}
} }

Main2Activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.example.wang.testapp2.Main2Activity"
android:orientation="vertical"> <TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ccc"
android:gravity="center_vertical"
android:textSize="25sp"
android:id="@+id/tv_1"/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="方法一"
android:id="@+id/bt_1"
android:onClick="bt_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="方法二"
android:id="@+id/bt_2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="方法三-1"
android:id="@+id/bt_31"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="方法三-2"
android:id="@+id/bt_32"/> </LinearLayout>

activity_main2

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wang.testapp2"> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ZuoyeActivity" />
<activity android:name=".MainActivity"></activity>
</application>
A
</manifest>

AndroidManifest

OnClickListener接口的更多相关文章

  1. Android中使用OnClickListener接口实现button点击的低级失误

    今天写了几行极为简单的代码,就是想implements  View.OnCLickListener.然后实现按钮点击操作.可是按钮却没有反应.找了五分钟还是没有结果. 下面是我的代码,希望大家不要嘲笑 ...

  2. Second Day: 关于Button监听事件的三种方法(匿名类、外部类、继承接口)

    第一种:通过匿名类实现对Button事件的监听 首先在XML文件中拖入一个Button按钮,并设好ID,其次在主文件.java中进行控件初始化(Private声明),随后通过SetOnClickLis ...

  3. Android开发 ---Button的OnClickListener的三种实现方法

    button的OnClickListener的三种实现方法 onclick事件的定义方法,分为三种,分别为 1.在xml中进行指定方法: 2.在Actitivy中new出一个OnClickListen ...

  4. button的OnClickListener的三种实现方法

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

  5. OnclickListener

    https://developer.android.com/reference/android/view/View.OnClickListener.html# https://blog.csdn.ne ...

  6. 回调深入理解 同步回调 以android中View.OnClickListener为列

    现在来分析分析下Android View的点击方法onclick();我们知道onclick()是一个回调方法,当用户点击View就执行这个方法,我们用Button来举例好了   //这个是View的 ...

  7. android中的OnClickListener两种实现方式

    android的activity点击事件中,通过OnClickListener来实现,要实现点击事件有两种方式 1.通过定义一个OnClickListener的内部类来实现 The example b ...

  8. Android接口回调的理解

    1.各种理解 <1>说白了,就是拿到对象引用,调其方法 <2>实际上就是利用多态的方式调用而已 <3>其实很容易理解的,定义接口,然后提供一个外部的接口设置进去,然 ...

  9. Android笔记——Button点击事件几种写法

    Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button ...

随机推荐

  1. 利用VisualStudio单元测试框架举一个简单的单元测试例子

    本随笔很简单,不涉及mock和stub对象,而是只给出一个简单的利用Visual Studio单元测试框架的最简单例子.如果需要深入理解Unit Test的原理与艺术,请参考<The art o ...

  2. R语言计算moran‘I

    R语言计算moran‘I install.packages("maptools")#画地图的包 install.packages("spdep")#空间统计,m ...

  3. Python之paramiko模块和SQL连接API

    堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: i ...

  4. windows配置Python多版本共存

    windows配置Python多版本共存 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近Python开发蛮火的,三年前我的一个运维朋友就告诉我说Python语言将来会很火,当时 ...

  5. Java poi读取,写入Excel2003

    Java poi读取,写入Excel2003 相关阅读:poi读写Excel2007:http://www.cnblogs.com/gavinYang/p/3576741.htmljxl读写excel ...

  6. python---基础知识回顾(十一)图像处理模块PIL

    前戏: 虽然PIL没有入OpenCV那样强大的功能,但是所提供的功能,在一般的图像处理中足够使用. 图像类别: 计算机绘图中有两类图像:一类是矢量图,另一类是点阵图(位图) 矢量图:基于计算机数字对象 ...

  7. 离线下载pip包安装

    Host-A 不能上网,但是需要在上面安装python-package 通过另外一台能上网的Host-B主机 1. 下载需要离线安装的Packages 在Host-B上执行如下命令: 安装单个Pack ...

  8. 令assignment操作符返回一个reference to *this

    [令assignment操作符返回一个reference to *this] 关于赋值,可以把它们写成连锁形式: int x, y, z; x =y =z =15; II赋值连锁形式 上述连锁赋值被解 ...

  9. 8、String练习题

    String练习   1.字符串反转,例如将"abc"变成"cba" 2.统计一个字符串里面另一个字符串出现的次数,例如统计"monkey" ...

  10. 《区块链100问》第75集:大零币Zcash是什么?

    Zcash,全称Zero Cash,简称ZEC,中文叫大零币,研发者为Zooko Wilcox,诞生于2011年11月9日. 采用零知识证明机制提供完全的支付保密性,是目前匿名性最强的数字资产.零知识 ...