有关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这样的阻塞程序 ...
随机推荐
- EF Code-First 学习之旅 多对多的关系
public class Student { public Student() { this.Courses = new HashSet<Course>(); } public int S ...
- share point 2013 部署
最近工作需要,需要用share point 2013 开发项目,但是遇到了刚部署环境就遇到了不少问题.唉~ 本机部署搭建一个share point 2013 环境. 第一步:首先要下载一个vmware ...
- springBean的作用域
Bean的作用域有五个类别 1.singleton,不写的话默认也是这个,这个的意思就是,单例的,就是说,不管你new多少次,都是一个对象 2.prototype,就是说每次new一个bean都是一个 ...
- PermutationsUnique,求全排列,去重
问题描述:给定一个数组,数组里面有重复元素,求全排列. 算法分析:和上一道题一样,只不过要去重. import java.util.ArrayList; import java.util.HashSe ...
- thinkphp <eq> <if>标签 condition中可以写PHP的判断逻辑
<ul> <volist name="monthArray" id="monthItem"> <if condition=&quo ...
- 认证和授权(Authentication和Authorization)
什么是OAuth 如今很多网站的功能都强调彼此间的交互,因此我们需要一种简单,标准的解决方案来安全的完成应用的授权,于是,OAuth应运而生,看看官网对其的定义: An open protocol t ...
- 《JavaScript高级程序设计》第7章 函数表达式
定义函数的方式有两种:函数声明和函数表达式 // 函数声明 function function_name(argument) { // body... } // 函数表达式 var function_ ...
- python3 on macos with vscode
brew install python3 python3 -m pip install pylint python3 -m pip install autopep8 python3 -m pip in ...
- Java基础13:反射与注解详解
Java基础13:反射与注解详解 什么是反射? 反射(Reflection)是Java 程序开发语言的特征之一,它允许运行中的 Java 程序获取自身的信息,并且可以操作类或对象的内部属性. Orac ...
- python:webbrowser
import webbrowser webbrowser.open_new_tab('www.baidu.com')