转:在android中button响应的两种方式
1.
在布局文件中添加button的监听名字
Android:onClick=”buttonOnClick”
例如:
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/textView2"
android:layout_marginTop="28dp"
android:text="Button"
android:onClick="buttonOnClick" />
在activity中创建响应的函数
publicvoid buttonOnClick(View view){
Mytext1.setText("hello!");//设置文档的显示
}
2.
在OnCreate函数中写
Mybutton = (Button)findViewById(R.id.button1);
Mybutton.setOnClickListener(new OnClickListener(){
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
Mytext.setText("hello!");//设置文档的显示
}
});
3.在OnCreate函数中写
findViewById(R.id.btn_title_popmenu).setOnClickListener(this);
findViewById(R.id.button1).setOnClickListener(this);
再来
publicvoid onClick(View v) {
if(v.getId() == R.id.btn_title_popmenu){
popMenu.showAsDropDown(v);
}elseif(v.getId()==R.id.button1)
{
popMenu1.showAsDropDown(v);
}
}
前提条件是:
public class MainActivity extends Activity implements OnClickListener{}
第三种方法例子:
publicclass MainActivity extends Activity implements OnClickListener{
private TextView Mytext = null;
private TextView Mytext1 = null;
@Override
//第一次运行activity时会运行
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//设置要显示的控件
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
Mytext = (TextView)findViewById(R.id.textView1);
Mytext1 = (TextView)findViewById(R.id.textView2);
}
@Override
publicvoid onClick(View arg0) {
// TODO自动生成的方法存根
if(arg0.getId()==R.id.button1){
Mytext.setText("hello!");//设置文档的显示
}
elseif(arg0.getId()==R.id.button2){
Mytext1.setText("hello!");//设置文档的显示
}
}
前两种方法的例子:
public class MainActivity extends Activity {
private Button Mybutton = null;
private Button Mybutton1 = null;
private TextView Mytext = null;
private TextView Mytext1 = null;
@Override
//第一次运行activity时会运行
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//设置要显示的控件
Mybutton = (Button)findViewById(R.id.button1);
Mytext = (TextView)findViewById(R.id.textView1);
Mytext1 = (TextView)findViewById(R.id.textView2);
//第二种方法
Mybutton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
Mytext.setText("hello!");//设置文档的显示
}
});
}
//第一种方法
public void button2OnClick(View view){
Mytext1.setText("hello!");//设置文档的显示
}
//第一种方法要在button的布局文件中设置android:onClick="buttonOnClick"
转:在android中button响应的两种方式的更多相关文章
- (原创)android中使用相机的两种方式
在社交类应用或扫描二维码的场合都需要用到手机上的摄像头 在程序中启用这一硬件主要有两类方法 1.发送intent启动系统自带的摄像应用 此应用的AndroidManifest中的intent-filt ...
- android中解析文件的三种方式
android中解析文件的三种方式 好久没有动手写点东西了,最近在研究android的相关技术,现在就android中解析文件的三种方式做以下总结.其主要有:SAX(Simple API fo ...
- linux内核分析作业4:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
系统调用:库函数封装了系统调用,通过库函数和系统调用打交道 用户态:低级别执行状态,代码的掌控范围会受到限制. 内核态:高执行级别,代码可移植性特权指令,访问任意物理地址 为什么划分级别:如果全部特权 ...
- Java中HashMap遍历的两种方式
Java中HashMap遍历的两种方式 转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: ...
- jQuery中开发插件的两种方式
jQuery中开发插件的两种方式(附Demo) 做web开发的基本上都会用到jQuery,jQuery插件开发两种方式:一种是类扩展的方式开发插件,jQuery添加新的全局函数(jQuery的全局函数 ...
- web.config文件中配置数据库连接的两种方式
web.config文件中配置数据库连接的两种方式 标签: 数据库webconfig 2015-04-28 18:18 31590人阅读 评论(1)收藏举报 分类: 数据库(74) 在网站开发 ...
- java中数组复制的两种方式
在java中数组复制有两种方式: 一:System.arraycopy(原数组,开始copy的下标,存放copy内容的数组,开始存放的下标,需要copy的长度); 这个方法需要先创建一个空的存放cop ...
- 实验--使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用(杨光)
使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用 攥写人:杨光 学号:20135233 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程 ...
- LInux内核分析--使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
实验者:江军 ID:fuchen1994 实验描述: 选择一个系统调用(13号系统调用time除外),系统调用列表参见http://codelab.shiyanlou.com/xref/linux-3 ...
随机推荐
- adb常用命令(手机测试)
ADB安装与常用命令详解 一.ADB意义 adb的全称为Android Debug Bridge,就是起到 ...
- Qt 报错LINK2019:无法解析的外部符号
这里用的都是Qt 自己的东西,但是还是抱错,所以怀疑是没有包含进去,尝试了清理项目,重新编译等,还是不行 用到一个最好的办法,就是把构建的文件夹整个删除,在重新编译就可以了 如图所示,把debug和r ...
- ThreadPool线程池的几种姿势比较
from multiprocessing.pool import ThreadPool #from multiprocessing.dummy import Pool as ThreadPool #这 ...
- GFS文件系统
1.1 分布式文件系统 1.1.1 什么是分布式文件系统 相对于本机端的文件系统而言,分布式文件系统(英语:Distributed file system, DFS),或是网络文件系统(英语:Ne ...
- kinit
su tf$ kinit -k -t /tmp/tf.keytab tf/admin@SINO.COM
- LeetCode 876——链表的中间结点
1. 题目 给定一个带有头结点 head 的非空单链表,返回链表的中间结点. 如果有两个中间结点,则返回第二个中间结点. 示例 1: 输入:[1,2,3,4,5] 输出:此列表中的结点 3 (序列化形 ...
- 目标检测之Faster-RCNN的pytorch代码详解(模型准备篇)
十月一的假期转眼就结束了,这个假期带女朋友到处玩了玩,虽然经济仿佛要陷入危机,不过没关系,要是吃不上饭就看书,吃精神粮食也不错,哈哈!开个玩笑,是要收收心好好干活了,继续写Faster-RCNN的代码 ...
- Kernel Mode, User Mode
之前关于kernel mode,user mode之间的切换,有个问题一直有些疑惑. 一个进程有没有办法,从user mode切换到kernel mode去执行自己的代码.我知道答案肯定是不行,但是为 ...
- Dubbo 的 Helloworld
前提条件 安装好了 ZooKeeper 作为注册中心 服务端 <?xml version="1.0" encoding="UTF-8"?> < ...
- [LINUX]警告:检测到时钟错误。您的创建可能是不完整的。
[LINUX]警告:检测到时钟错误.您的创建可能是不完整的. 原因: 如果上一次编译时为20071001,你把系统时间改成20070901后再编译就会报这样的错误. 解决: 把时间 ...