关于设置listener监听onClicked事件的步骤分析

Steps:

1.tell android you are interested in listening to a button click

2.bring your xml button inside java

3.tell your java button whose responding when it's clicked

4.what should happen when button is clicked

     <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="@+id/button1"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
 public class MainActivity extends Activity implements OnClickListener {//1.tell android you are interested in listening to a button click

     Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);//2.bring your xml button inside java
button.setOnClickListener(this);//3.tell your java button whose responding when it's clicked
} @Override//4.what should happen when button is clicked
public void onClick(View v) {
Log.e("MainActivity","Clicked1");
} }

监听多个button:

         button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(this);
button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(this); @Override
public void onClick(View view) {
if(view.getId()==R.id.button1){//复制
TextView textView = (TextView)findViewById(R.id.textView);
TextView textView1 = (TextView)findViewById(R.id.editText);
textView.setText(""+textView1.getText()); }
if(view.getId()==R.id.button2){//锁定
TextView textView = (TextView)findViewById(R.id.editText);
//textView.setFocusable(false);
//textView.setFocusableInTouchMode(false);
//textView.setEnabled(false);
keyListener=textView.getKeyListener();
textView.setKeyListener(null);
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);//这两行是收起软键盘
imm.hideSoftInputFromWindow(textView.getWindowToken(),0); }
if(view.getId()==R.id.button3){//编辑
TextView textView = (TextView)findViewById(R.id.editText);
textView.setKeyListener(keyListener);
textView.setFocusable(true);
textView.setFocusableInTouchMode(true);
textView.requestFocus(); }
}

布局如下:

 <TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复制"
android:id="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="锁定"
android:id="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="编辑"
android:id="@+id/button3"
android:layout_centerHorizontal="true" />

有关Botton的用法(二)的更多相关文章

  1. react基础用法二(组件渲染)

    react基础用法二(组件渲染) 如图所示组件可以是函数 格式:function 方法名(){ return <标签>内容</标签>} 渲染格式: <方法名 />  ...

  2. Java关于Properties用法(二)——替换配置文件中的参数

    上一章讲了配置文件的基本用法,虽然上一章已经可以解决一些需求,但还不些不足之处.假如,配置文件里面的字符串有一部分需要经常变动,另外一些不需要,上一章的方法就不方便了,所以这章主要讲如何在配置文件中使 ...

  3. 关于Unity中的刚体和碰撞器的相关用法(二)

    在关于Unity中的刚体和碰撞器的相关用法(一)的基础上 有一个plane平面,一个ball球体,都挂了碰撞器,ball挂了刚体Rigidbody,写了一个脚本ball挂载在球体上,球体从空中落下装机 ...

  4. requirejs的用法(二)

    这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的 ...

  5. Dapper学习 - Dapper的基本用法(二) - 存储过程/函数

    上一篇貌似少介绍了自定义函数和存储过程, 因为这两个也可以使用查询的方式来实现功能, 这一篇就补上 一.自定义函数的创建和调用 (mysql的) Delimiter $$ drop function ...

  6. ReSharper 配置及用法(二)

    下载工具 一:Reshaper是什么 即便是那些整天攻击 .NET 和 C# 的人,也常常不得不承认 Visual Studio 确实是个够强大的 IDE,除非他认为更少的 IDE 功能和命令行调试才 ...

  7. javaweb学习总结二十六(response对象的用法二 下载文件)

    一:浏览器打开服务器上的文件 1:读取服务器上面的资源,如果在web层,可以直接使用servletContext,如果在非web层 可以使用类加载器读取文件 2:向浏览器写数据,实际上是把数据封装到r ...

  8. JavaScript高级用法二之内置对象

    综述 本篇的主要内容来自慕课网,内置对象,主要内容如下 1 什么是对象 2 Date 日期对象 3 返回/设置年份方法 4 返回星期方法 5 返回/设置时间方法 6 String 字符串对象 7 返回 ...

  9. select()函数用法二

    Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程序 ...

随机推荐

  1. openlayers2地图控件扩展:图例控件LegendControl

    因项目需要在地图中增加图例,以便专题地图查看或输出. 实现思路,折线和多边形图例直接由样式属性创建,多边形直接设置div的样式:折线通过创建svg,设置polyline的样式:点要素的图例比较复杂,目 ...

  2. Python 有点意思

    基本语法 >>> width = 20 >>> height = 2 * 3 >>> width * height 120 >>> ...

  3. JavaScript 打印控件

    JavaScript 打印控件 github地址 https://github.com/DoersGuild/jQuery.print 使用前需要引入jQuery $("#mapDiv&qu ...

  4. SSH免密登录配置

    SSH免密登录配置 本地生成密钥文件: $ ssh-keygen 输出: Generating public/private rsa key pair. Enter file in which to ...

  5. Ajax-02 iframe实现伪“Ajax”

    需求: 用户输入URL,使用iframe将目标URL的内容加载到页面指定位置(局部刷新) <!DOCTYPE html> <html lang="en"> ...

  6. java JVM 随笔

    先说重点: 对象在堆区 方法在栈区 变量在方法区,常量池在方法区 为什么要了解Java 虚拟机 ? 这个问题一直困惑了我很长一段时间,其实在我们开发的过程中,即使我们不了解JVM也能正常的开发,但是当 ...

  7. hdu 5265

    http://acm.hdu.edu.cn/showproblem.php?pid=5256 题目不错,题面忍不住骂一句mmp.......后面说ai都是正整数,我以为修改后也必须是正整数,前面又说只 ...

  8. 51nod 1042 数位dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042 1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131 ...

  9. python面向对象( item系列,__enter__ 和__exit__,__call__方法,元类)

    python面向对象进阶(下)   item系列 __slots__方法 __next__ 和 __iter__实现迭代器  析构函数 上下文管理协议 元类一.item系列 把对象操作属性模拟成字典的 ...

  10. flask bootstrap IE11的 兼容性问题

    在子模板内添加如下代码确保IE11的兼容性问题. {% extends 'bootstrap/base.html' %}{% block metas %} <meta http-equiv=&q ...