转:在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 ...
随机推荐
- MongoDB复制二:复制集的管理
1.修改oplog的大小 需要在每个机器上都配置.先在secondary上操作,最后在primary上操作. 1)以单机的方式重启复制集的实例 db.shutdownServer() 在新的端口中启 ...
- HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)
Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...
- 【翻译】ASP.NET Core 文档目录
微软官方CORE 2.0正式版中文文档已经出来了,地址:https://docs.microsoft.com/zh-cn/aspnet/core/ 简介 入门 创建一个Web应用程序 创建一个Web ...
- 用OneNote写博客的方法
1.进入OneNote要发布博客的分区然后点击菜单栏中的文件 2.点击发送至博客 3.这时候会启动word程序弹出下面的对话框(如果你从未设置过)点击立即注册 ...
- java线程(6)——线程池(下)
上篇博客java线程(5)--线程池(上)介绍了线程池的基本知识,这篇博客我们介绍一下常用的ThreadPoolExecutor. 定义 类图关系: ThreadPoolExecutor继承了Abst ...
- 【SSH】——Struts2中的动态方法调用(一)
首先我们来看一个简单的调用: 1.在web.xml中配置拦截器StrutsPrepareAndExecuteFilter.StrutsPrepareAndExecuteFilter实现了filter接 ...
- Win10 1803安装Ubuntu1804子系统
1.win10应用商店选择Ubuntu1804安装 点击打开会提示https://docs.microsoft.com/zh-cn/windows/wsl/install-win10 2.用管理员po ...
- Windows IRP
IRP(I/O Request Packet),是由IO manager发起的对device的IO请求. 当用户调用系统API,如createFile类似的函数,其实是会交给IO manager来做相 ...
- WCF 动态调用(动态创建实例接口)
很多时候,服务地址都不止一个的,这个时候就要动态去配置地址.配置Web.config,很麻烦 下面就看看怎样实现动态调用WCF. 首先看看动态创建服务对象的代码: using System; usin ...
- PHP判断类型的方法
1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...