高明鑫《最新Android入门课程》课堂笔记四
来源:互联网 发布:c语言两个等于号 编辑:程序博客网 时间:2024/06/05 07:51
课堂8 常见事件响应的实现方式
1.单击事件
onClickListener
2.长按事件
onLongClickListener
3.滑动事件
onTouchListener
4.键盘事件
onKeyListener
5.焦点事件
setOnFocusChangeListener
①动态设置(最常用的方式)
button.setOnClickListener(new OnClickListener() { @Override <span style="white-space:pre"></span>public void onClick(View v){ System.println("button发生点击事件!!");<span style="white-space:pre"></span>} });
</pre><p><span class="token p" style="outline:none!important">②配置方式,仅限于onclick</span><span class="token lf" style="outline:none!important"></span><span class="token p" style="outline:none!important"> </span></p><p><span class="token p" style="outline:none!important"></span></p><pre name="code" class="java"><pre name="code" class="java"><Button android:id="@+id/button2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button" android:onClick="test2" />
就是当button2被点击时,去找到一个叫test2的方法执行。
在Activity中实现test2方法如下:
public void test2(View view){
}
③可复用方式在onclick方法中判断哪个组件发生了相应的事件
Button button1 = (Button) findViewById(R.id.button1); Button button2 = (Button) findViewById(R.id.button2); Button button3 = (Button) findViewById(R.id.button3);
<pre name="code" class="java">button1.setOnClickListener(myOnclickListener); button2.setOnClickListener(myOnclickListener); button3.setOnClickListener(myOnclickListener);
<pre name="code" class="java"><pre name="code" class="java">private View.OnClickListener myOnclickListener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: Toast.makeText(MainActivity.this, "button1被点击啦。。。", Toast.LENGTH_SHORT).show(); break; case R.id.button2: Toast.makeText(MainActivity.this, "button2被点击啦。。。", Toast.LENGTH_SHORT).show(); break; case R.id.button3: Toast.makeText(MainActivity.this, "button3被点击啦。。。", Toast.LENGTH_SHORT).show(); break; default: break; } }}
多个按钮点击事件,只需实现一次onCLickListener方法。
0 0
- 高明鑫《最新Android入门课程》课堂笔记四
- 高明鑫《最新Android入门课程》课堂笔记一
- 高明鑫《最新Android入门课程》课堂笔记二
- 高明鑫《最新Android入门课程》课堂笔记三
- 课堂笔记四
- Hash 入门课堂笔记
- 浅谈慕课网React入门课程笔记(四)
- [课堂笔记]四.面向对象
- day01:Android课堂笔记
- 树型 DP 入门课堂笔记
- 机器学习课堂笔记(四)
- Android ui 测试课堂笔记
- android入门:zxing学习笔记(四)
- android入门:zxing学习笔记(四)
- android入门:zxing学习笔记(四)
- Android入门笔记之源码解析四
- android入门:zxing学习笔记(四)
- Android入门学习笔记整理(四)
- a的n次幂 —— POJ 3100 Root of the Problem
- 基础四:ContentProvider
- thrift 库使用心得
- shareSDK 新浪取消授权
- 第四周-项目3 - 单链表应用(3)判断递增
- 高明鑫《最新Android入门课程》课堂笔记四
- java 生成 word方案
- Android开发中怎样调用系统Email发送邮件(多种调用方式)
- hdu 1937 Finding Seats
- Java 装箱和拆箱
- Bluetooth SIG profiles
- Android 自定义 DialogFragment 宽度问题
- hihocoder 1233 Boxes(bfs+状态压缩+hash表)
- linux中对文件名包含空格时处理异常的解决方法