一步一步学android之事件篇——单击事件
在使用软件的时候单击事件必不可少,比如我想确定、取消等都需要用户的单击,所有的单击事件都是由View.OnClickListener接口来进行处理的,接口定义如下:
public static interface View.OnClickListener{
public void onClick(View v) {
}
}
下面同样写个例子来看下如何监听单击事件:
运行效果:
单击前的效果
单击后的效果
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入信息...">
</EditText> <Button
android:id="@+id/bt_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="显示EditText里面的内容到下面的TextView中" /> <TextView
android:id="@+id/tv_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我是用来显示EditText内容的TextView,点击上面的按钮我就能显示" /> </LinearLayout>
MainActivity.java:
package com.example.clickdemo; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity {
//定义三个用到的组件
private Button bt_show;
private EditText info;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
//单独写个方法初始化组件
private void initView(){
//从xml中获取Button,xml中id的作用
bt_show = (Button)super.findViewById(R.id.bt_show);
//从xml中获取EditText
tv_show = (TextView)super.findViewById(R.id.tv_show);
//从xml中获取TextView
info = (EditText)super.findViewById(R.id.info); //为Button按钮添加单击事件
bt_show.setOnClickListener(new MyClickListener());
}
//定义监听处理程序
private class MyClickListener implements OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
//首先获取EditText中的内容
String temp = info.getText().toString();
//再把内容设置到TextView中
tv_show.setText(temp);
} } }
第一次
使用findViewById()方法,说明一下这个方法的作用是取得在xml中的组件,如果没取组件就用会报空指针异常,然后是使用setOnClickListener()方法来为对按钮监听,这里是定义了一个内部类来实现OnClickListener接口,然后在OnClick方法中实现自己的逻辑(单击后要做的事情)。
上面的代码还可以修改为不使用内部类,毕竟我们只用一次,所以改成下面的使用匿名内部类的方法:
MainActivity.java:
package com.example.clickdemo; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity {
//定义三个用到的组件
private Button bt_show;
private EditText info;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
//单独写个方法初始化组件
private void initView(){
//从xml中获取Button,xml中id的作用
bt_show = (Button)super.findViewById(R.id.bt_show);
//从xml中获取EditText
tv_show = (TextView)super.findViewById(R.id.tv_show);
//从xml中获取TextView
info = (EditText)super.findViewById(R.id.info); //为Button按钮添加单击事件
bt_show.setOnClickListener(new OnClickListener() { public void onClick(View v) {
// TODO Auto-generated method stub
//首先获取EditText中的内容
String temp = info.getText().toString();
//再把内容设置到TextView中
tv_show.setText(temp);
}
});
}
}
效果是一样的,今天就说到这里了。
一步一步学android之事件篇——单击事件的更多相关文章
- xamarin android如何监听单击事件
在xamarin android单击事件是最基础的事情,看过菜鸟上的android教程时,java写的都是监听事件,为一个按钮,单选按钮.多选按钮的单击事件有三种,前面两种用的非常普遍,也很简易,我这 ...
- Android代码规范----按钮单击事件的四种写法
[前言] 按钮少的时候用第三种的匿名内部类会比较快,比如写demo测试的时候或者登陆界面之类. 按钮多的时候一般选择第四种写法. 一.第一种写法:在XML文件中声明onClick属性(很少用) 在XM ...
- 第二十六篇-单击事件、Toast(提示框信息)
单击事件有3种方法: 第一种: layout.xml <?xml version="1.0" encoding="utf-8"?> <Line ...
- jQuery-3.事件篇---鼠标事件
jQuery鼠标事件之click与dbclick事件 用交互操作中,最简单直接的操作就是点击操作.jQuery提供了两个方法一个是click方法用于监听用户单击操作,另一个方法是dbclick方法用于 ...
- jQuery-3.事件篇---自定义事件
jQuery自定义事件之trigger事件 众所周知类似于mousedown.click.keydown等等这类型的事件都是浏览器提供的,通俗叫原生事件,这类型的事件是需要有交互行为才能被触发. 在j ...
- jQuery-3.事件篇---键盘事件
jQuery键盘事件之keydown()与keyup()事件 鼠标有mousedown,mouseup之类的事件,这是根据人的手势动作分解的2个触发行为.相对应的键盘也有这类事件,将用户行为分解成2个 ...
- jQuery事件篇---高级事件
内容提纲: 1.模拟操作 2.命名空间 3.事件委托 4.on.off 和 one 发文不易,转载请注明出处! 一.模拟操作 在事件触发的时候,有时我们需要一些模拟用户行为的操作.例如:当网页加载完毕 ...
- jQuery事件篇---基础事件
写在前面: 有一段时间未更新博客了,利用这段时间,重新看了<jQuery基础教程 第四版>和<锋利的jQuery 第二版>,这两本书绝对是jQuery入门非常好的书,值得多读几 ...
- JS事件 鼠标单击事件( onclick )通常与按钮一起使用。onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件。同时onclick事件调用的程序块就会被执行
鼠标单击事件( onclick ) onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件.同时onclick事件调用的程序块就会被执行,通常与按钮一起使用. 比如,我们单击按钮时,触发 ...
随机推荐
- webservice发送数据,取数据的方式
1.通过调用对方的webservice接口方式,取得对方的数据,并解析(我们取数据) 2.对方调用我们的接口,得到数据.(对方来取) 3.对用对方接口,将我们的数据封装好以后,直接调用对方接口,对方可 ...
- oracle数据表误删恢复
1.查看回收站中的表: select object_name,original_name,partition_name,type,ts_name,createtime,droptime from re ...
- XE6 /XE8 & IOS开发之免证书真机调试三步走,生成iPA文件并安装到其它苹果设备上
XE6 & IOS开发之免证书真机调试(1):颁发属于自己的App签名证书(有图有真相) XE6 & IOS开发之免证书真机调试(2):连接真机并运行App(有图有真相) XE6 &a ...
- SGU 137.Funny String
题目描述 一个序列S1 S2 S3... Sn 如果满足 新序列 S1-1 S2 S3 ...Sn+1能够通过旋转的操作(不是翻转)来得到旧的序列,那么这个序列就叫做Funny序列.例如 1 2 1 ...
- Tenth Line
How would you print just the 10th line of a file? For example, assume that file.txt has the followin ...
- Java学习----集合框架总结
集合框架总结: Collection接口: Set接口: HashSet//对象必须实现hashCode方法,元素没有顺序呢,效率比LinkedHashSet高 LinkedHashSet//是Has ...
- underscorejs-size学习
2.24 size 2.24.1 语法: _.size(list) 2.24.2 说明: 返回列表的长度. 示例一:返回数组.对象.字符串的长度 //取数组的长度 var length length ...
- ubuntu更新源
源一定要找对应的版本 14.04对应 trusty deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multive ...
- hdu不要62
Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来, ...
- Whitespace character
In computer science, whitespace is any character or series of whitespace characters that represent h ...