有关Botton的用法(二)
关于设置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的用法(二)的更多相关文章
- react基础用法二(组件渲染)
react基础用法二(组件渲染) 如图所示组件可以是函数 格式:function 方法名(){ return <标签>内容</标签>} 渲染格式: <方法名 /> ...
- Java关于Properties用法(二)——替换配置文件中的参数
上一章讲了配置文件的基本用法,虽然上一章已经可以解决一些需求,但还不些不足之处.假如,配置文件里面的字符串有一部分需要经常变动,另外一些不需要,上一章的方法就不方便了,所以这章主要讲如何在配置文件中使 ...
- 关于Unity中的刚体和碰撞器的相关用法(二)
在关于Unity中的刚体和碰撞器的相关用法(一)的基础上 有一个plane平面,一个ball球体,都挂了碰撞器,ball挂了刚体Rigidbody,写了一个脚本ball挂载在球体上,球体从空中落下装机 ...
- requirejs的用法(二)
这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的 ...
- Dapper学习 - Dapper的基本用法(二) - 存储过程/函数
上一篇貌似少介绍了自定义函数和存储过程, 因为这两个也可以使用查询的方式来实现功能, 这一篇就补上 一.自定义函数的创建和调用 (mysql的) Delimiter $$ drop function ...
- ReSharper 配置及用法(二)
下载工具 一:Reshaper是什么 即便是那些整天攻击 .NET 和 C# 的人,也常常不得不承认 Visual Studio 确实是个够强大的 IDE,除非他认为更少的 IDE 功能和命令行调试才 ...
- javaweb学习总结二十六(response对象的用法二 下载文件)
一:浏览器打开服务器上的文件 1:读取服务器上面的资源,如果在web层,可以直接使用servletContext,如果在非web层 可以使用类加载器读取文件 2:向浏览器写数据,实际上是把数据封装到r ...
- JavaScript高级用法二之内置对象
综述 本篇的主要内容来自慕课网,内置对象,主要内容如下 1 什么是对象 2 Date 日期对象 3 返回/设置年份方法 4 返回星期方法 5 返回/设置时间方法 6 String 字符串对象 7 返回 ...
- select()函数用法二
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如 connect.accept.recv或recvfrom这样的阻塞程序 ...
随机推荐
- 并发-Synchronized底层优化(偏向锁、轻量级锁)
Synchronized底层优化(偏向锁.轻量级锁) 参考: http://www.cnblogs.com/paddix/p/5405678.html 一.重量级锁 上篇文章中向大家介绍了Synchr ...
- 多线程-栅栏CyclicBarrier
上一篇总结了闭锁CountDownLatch,这一篇总结一下栅栏CyclicBarrier.它们两者之间的区别主要是,闭锁是等待一个事件发生,比如上一篇的田径比赛,运动员等待裁判哨声一响就可以开始跑, ...
- Codeforces Round #315 (Div. 2) C. Primes or Palindromes? 暴力
C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input st ...
- easyui扩展数据表格点击加号拓展
$(function(){ $("#RepaymentInfoTab").datagrid({ view: detailview, detailFormatter:function ...
- Linux find 命令大全
find 含义: 顾名思义,是从来查找满足条件的内容. 从指定目录,递归的查找满足条件的内容. 格式: find [查询目录] [参数] [匹配方式] 文件操作: -name : 查找文件名 ( f ...
- WPF中的事件列表 .
以下是WPF中的常见事件汇总表(按字母排序),翻译不见得准确,但希望对你有用. 事件 描述 Annotation.AnchorChanged 新增.移除或修改 Anchor 元素时发生. Annota ...
- 分享知识-快乐自己:MYSQL之內链接 左链接 右链接 区别
MYSQL中可以通过内外键链接,将有关系的表中数据合并到一起进行条件筛选: 首先创建两个新表,数据如下: student 表数据: score 表数据: 可以看到students表中stu_id为16 ...
- WEKA运行参数修改(RunWeka.ini文件)
一般使用weka进行数据挖掘的时候会碰到两个问题,一是内存不够,二是libsvm使用不了,这时就需要重新配置RunWeka.ini文件,解决上述问题.查看RunWeka.ini原文如下: # Cont ...
- hexo多主题切换
今天看到一个朋友在github上面的issue 大概问题就是怎么在不同的电脑上面使用 git有个这么个东西Submoudle中文叫做子模块 具体使用教程看这里Git-工具-子模块 这里只说怎么搞hex ...
- eclipse javaw.exe in your current path问题
问题: 第一次运行eclipse的时候,可能会提醒找不到javaw.exe ******等的问题 很坑的! 解决方案: 无法启动Eclipe,因找不到javaw.exe 还是环境变量的问题!!! 注意 ...