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. Jmeter之性能测试

    Jmeter除了可以做接口测试外,还可以做性能测试.在 Jmeter中做性能测试,需要做如下相关设置 图片中有10个线程,Ramp-Up Period(in seconds)=1,那么线程的启动时间间 ...

  2. ElasticStack系列之八 & _source 字段

    有很多人会有这样的一个疑问: _source字段存储的是索引的原始内容,那 store 属性的设置是为何呢?elasticsearch 为什么要把 store 的默认取值设置为 no?设置为 yes ...

  3. day9 类、对象、包

    结构化编程中,程序围绕要解决的问题来设计. 面向对象编程,围绕要解决问题的对象来设计. 万物皆对象,对象因关注而产生!!! 类——抽取具有相同属性和行为的对象. 属性就是对象身上的值数据,行为就是对象 ...

  4. servlet程序使用tomcat启动报错

    根据书上的需求写了一些简单的servlet代码,启动时报错: 严重: A child container failed during startjava.util.concurrent.Executi ...

  5. 2016/1/3 Python中的多线程(2):threading模块

    之前提了Python多线程的一点使用,今天介绍更好的threading模块,它提供了Thread类和一些比较好用的同步机制. 先介绍Thread类 threading模块中的Thread类有很多thr ...

  6. Document对象中的一些重要的属性和方法(笔记)

    Document对象:每一个web浏览器窗口.标签页和框架由一个window对象所表示.每个window对象都有一个document属性引用的是Document对象,它是一个巨大的API中的核心对象, ...

  7. 《大型网站SEO优化实践》学习分享

    本文主要内容源自2013年阿里技术嘉年华中阿里巴巴周文君分享<大型网站SEO优化实践>.学习过后,受益匪浅,特作笔记,经常回顾吸收学习. 大型网站SEO的特点&优势&挑战 ...

  8. vuex的安装

    可以启动vue ui 手动添加vuex. 或使用 cnpm install vuex 2.使用,import vuex from “vuex” vue.use(vuex) 3.安装插件, 首先键入谷歌 ...

  9. C#反射-Assembly.Load、LoadFrom与LoadFile

    反射Demo: public class Person { public int Age; public void SayHello() { Console.WriteLine("Hello ...

  10. jquery ajax complete 方法

    jquery ajax var ajaxTimeoutTest = $.ajax({ url:'',  //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'g ...