关于设置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. linux top ps 命令

    http://javawind.net/p131 VIRT:virtual memory usage 虚拟内存1.进程“需要的”虚拟内存大小,包括进程使用的库.代码.数据等2.假如进程申请100m的内 ...

  2. CodeChef FORESTGA 二分

    Forest Gathering   Problem code: FORESTGA Tweet     ALL SUBMISSIONS All submissions for this problem ...

  3. JNI_Z_05_方法的操作(没有String类型的参数)

    1.步骤: (1).获取 jclass (2).获取 method的id (3).调用 method ZC: 貌似 JNI里面 操作 类的方法,完全是 无视 访问权限的... 然而 static的方法 ...

  4. ZC_02_获取Constructor

    1. package reflectionZ; import java.lang.reflect.Constructor; import java.lang.reflect.Type; public ...

  5. jquery.js和jquery.min.js的区别介绍

    1.区别:jquery官网提供2种jQuery的下载,一种是jquery.js另一种是jquery.min.js文件名不一定完全相同,但通常情况下:jquery.js是完整的未压缩的jQuery库,文 ...

  6. CSLA验证规则总结

    CSLA业务规则 验证规则所在空间: Csla.Rules 基类 BusinessBase 的属性   BusinessRules 中记录了业务类的验证规则 验证规格的写法 private class ...

  7. HYSBZ - 2301 莫比乌斯反演

    链接 题解:直接用公式算,用容斥来减掉重复计算的部分 但是我犯了一个非常sb的错误,直接把abcd除k了,这样算a-1的时候就错了,然后举的例子刚好还没问题= = ,结果wa了好几发 //#pragm ...

  8. 【lightoj-1025】The Specials Menu(区间DP)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1025 [题目大意] 求一个字符串删去任意字符可以构成多少个不同的回文串 [分析 ...

  9. Http请求get和post调用

    工作中会遇到远程调用接口,需要编写Http请求的共通类 以下是自己总结的Http请求代码 package com.gomecar.index.common.utils; import org.apac ...

  10. 【python】使用asyncore进行异步通信

    参考博文:http://blog.csdn.net/livefun/article/details/8721772 参考博文:https://www.cnblogs.com/tomato0906/ar ...