OnClickListener接口
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接口的更多相关文章
- Android中使用OnClickListener接口实现button点击的低级失误
今天写了几行极为简单的代码,就是想implements View.OnCLickListener.然后实现按钮点击操作.可是按钮却没有反应.找了五分钟还是没有结果. 下面是我的代码,希望大家不要嘲笑 ...
- Second Day: 关于Button监听事件的三种方法(匿名类、外部类、继承接口)
第一种:通过匿名类实现对Button事件的监听 首先在XML文件中拖入一个Button按钮,并设好ID,其次在主文件.java中进行控件初始化(Private声明),随后通过SetOnClickLis ...
- Android开发 ---Button的OnClickListener的三种实现方法
button的OnClickListener的三种实现方法 onclick事件的定义方法,分为三种,分别为 1.在xml中进行指定方法: 2.在Actitivy中new出一个OnClickListen ...
- button的OnClickListener的三种实现方法
onclick事件的定义方法,分为三种,分别为在xml中进行指定方法:在Actitivy中new出一个OnClickListenner():实现OnClickListener接口三种方式. 代码分别如 ...
- OnclickListener
https://developer.android.com/reference/android/view/View.OnClickListener.html# https://blog.csdn.ne ...
- 回调深入理解 同步回调 以android中View.OnClickListener为列
现在来分析分析下Android View的点击方法onclick();我们知道onclick()是一个回调方法,当用户点击View就执行这个方法,我们用Button来举例好了 //这个是View的 ...
- android中的OnClickListener两种实现方式
android的activity点击事件中,通过OnClickListener来实现,要实现点击事件有两种方式 1.通过定义一个OnClickListener的内部类来实现 The example b ...
- Android接口回调的理解
1.各种理解 <1>说白了,就是拿到对象引用,调其方法 <2>实际上就是利用多态的方式调用而已 <3>其实很容易理解的,定义接口,然后提供一个外部的接口设置进去,然 ...
- Android笔记——Button点击事件几种写法
Button点击事件:大概可以分为以下几种: 匿名内部类 定义内部类,实现OnClickListener接口 定义的构造方法 用Activity实现OnClickListener接口 指定Button ...
随机推荐
- MySQL 快速入门
MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格 ...
- JS中的getter与setter
一.什么是getter和setter getter 是一种获得属性值的方法,setter是一种设置属性值的方法 getter负责查询值,它不带任何参数,setter则负责设置键值,值是以参数的形式传递 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...
- seaborn基础整理
seaborn是基于matplotlib的更高级的做图工具,下面主要针对以下几个部分进行整理: 第一部分:https://douzujun.github.io/page/%E6%95%B0%E6%8D ...
- ZeroMQ API(六) 代理
1.zmq_proxy(3) 1.1 名称 zmq_proxy - 开始内置ZMQ代理 1.2 概要 int zmq_proxy(const void * frontend,const void * ...
- nodejs express 上传文件 (格式 FormData)
前台代码使用jQuery的ajax: <script type="text/javascript"> $(function(){ $('#file_upload').c ...
- Oracle GoldenGate常用参数
OGG(Oracle GoldenGate)参数介绍 所有的GoldenGate进程均有参数文件 Manager Extract Replicat Utilities 所有参数均有缺省配置 实际应用只 ...
- 基于Ubuntu搭建GMS测试环境
一.版本信息: 系统版本:Ubuntu 18.04.2 LTS JDK版本: 1.8.0_171 SDK版本:android-sdk_r24.4.1-linux.tgz ADB版本:1.0.40 ap ...
- java.lang.IllegalArgumentException: class com.beisheng.maerte.mode.MyCouponVO declares multiple JSON fields named count
原因是:子类和父类有相同的字段属性.解决办法:(1)将父类中的该字段去掉(不要),或者在需要打印的字段上加上注解@Expose (2):由于我报错的类都是在jar包里面,所以第一种方法不好使.只好采用 ...
- 技术分享:如何在PowerShell脚本中嵌入EXE文件
技术分享:如何在PowerShell脚本中嵌入EXE文件 我在尝试解决一个问题,即在客户端攻击中只使用纯 PowerShell 脚本作为攻击负荷.使用 PowerShell 运行恶意代码具有很多优点, ...