关于设置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. mysql一次性删除所有表而不删除数据库

    1.执行如下语句获取删除语句 SELECT CONCAT( 'drop table ', table_name, ';' ) from information_schema.tables where ...

  2. springBean获取的几种方法

    1.通过FileSystemApplicationContext来获取(不常用,因为要spring配置文件的绝对路径) 2.通过ClassPathXmlApplicationContext来获取(常用 ...

  3. tomcat官方下载连接——安装版&绿色版

    Tomcat绿色版Windows64位9.0.10 http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.10/bin/apache-tomcat- ...

  4. VLAN虚拟局域网技术(一)-计算机网络

    本文主要知识来源于学校课程,部分知识来自于H3C公司教材,未经许可,禁止转载.如需转载,请联系作者并注明出处. 1.  VLAN(Virtual LAN):我们称之为虚拟局域网,它的作用就是将物理上互 ...

  5. Pandas级联

    Pandas提供了各种工具(功能),可以轻松地将Series,DataFrame和Panel对象组合在一起. pd.concat(objs,axis=0,join='outer',join_axes= ...

  6. OnTouch关于performClick的Warning

    OnTouch关于performClick的Warning 当你对一个控件(例如FloatingActionButton)使用setOnTouchListener() 或者是对你的自定义控件重写onT ...

  7. 【Demo】CSS3 3D转换

    3D转换transform rotateX() 方法 rotateX()方法,围绕其在一个给定度数X轴旋转的元素. div { transform: rotateX(120deg); -webkit- ...

  8. 51nod-1636-dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1636 1636 教育改革 题目来源: CodeForces 基准时间限制 ...

  9. CSS控制文字只显示一行,超出部分显示省略号

    <p style="width: 300px;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;"&g ...

  10. DDOS工具合集---CC 2.0(僵尸网络proxy,单一url,可设置cookie,refer),传奇克星(代理+单一url,可设置cookie),NetBot_Attacker网络僵尸1.0(僵尸网络,HTTP NO-Cache Get攻击模式,CC攻击,HTTP空GET请求攻击),傀儡僵尸VIP1.4版(僵尸网络,动态单一url)、上兴网络僵尸2.3、中国制造网络僵尸、安全基地网络僵尸==

    DDOS工具合集 from:https://blog.csdn.net/chinafe/article/details/74928587 CC 著名的DDOS CC工具,效果非常好!CC 2.0使用了 ...