为了实现近期录制的长按,松开手指,结束录制功能。在项目,难道你去走一走会头晕,书写demo为了下一个梳理。

顺便研究android事件调用机制。

先上效果界面:

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_centerInParent="true"
android:background="@drawable/microp_btn_normal" /> </RelativeLayout>

代码:

package com.example.androidtest;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent; public class MainActivity extends Activity { private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn); btn.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View arg0, MotionEvent arg1) {
Log.d("TAG", "onTouch is called.............");
return false;
}
}); btn.setOnLongClickListener(new OnLongClickListener() { @Override
public boolean onLongClick(View arg0) {
Log.d("TAG", "onLongClick is called.............");
return false;
}
}); btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Log.d("TAG", "onClick is called.............");
}
});
} }

在代码中。我们监听了onTouch、onLongClick、onClick事件,当中onTouch和OnLongClick有返回值,onClick没有返回值。

现执行以上代码,长按button。我们能够看到调用顺序为

onTouch->onLongClick->onClick

现将OnTouch的返回值改为true,可发现onLongClick和onClick事件没有调用。

说明onTouch返回true,兴许事件没有传递。

接下来我们看下onTouch详细触发了哪些事件(仅仅长按)

btn.setOnTouchListener(new OnTouchListener() {

			@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
//Log.d("TAG", "onTouch is called.............");
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("TAG", "ACTION_DOWN.............");
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG", "ACTION_MOVE.............");
break;
case MotionEvent.ACTION_CANCEL:
Log.d("TAG", "ACTION_CANCEL.............");
break;
case MotionEvent.ACTION_UP:
Log.d("TAG", "ACTION_UP.............");
break;
default:
break;
}
return false;
}
});

看下日志截图

我仅仅是按住,并未滑动。

我们将onLongClick返回true。能够预见,onClick事件不会被触发。

但ACTION_UP呢?

经验证ACTION_UP被触发了。

接下来我们换下布局文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View android:layout_width="match_parent"
android:layout_height="1000dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_gravity="center_horizontal"
android:background="@drawable/microp_btn_normal" /> </LinearLayout>
</ScrollView>

意图非常easy,让我们的button在滚动栏的底部,日志截图

这时,我们的ACTION_UP没被触发,最后触发的是ACTION_CANCLE,并且我按了好几次才让它触发onLongClick事件。大多数仅仅触发了DOWN/MOVE/CANCLE事件。

我怀疑是ScrollView滑动事件与onTouch事件冲突

在onTouch加上一句防冲突代码

btn.setOnTouchListener(new OnTouchListener() {

			@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
//Log.d("TAG", "onTouch is called.............");
arg0.getParent().requestDisallowInterceptTouchEvent(true);//通知父控件勿拦截本控件touch事件
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("TAG", "ACTION_DOWN.............");
break;
case MotionEvent.ACTION_MOVE:
Log.d("TAG", "ACTION_MOVE.............");
break;
case MotionEvent.ACTION_CANCEL:
Log.d("TAG", "ACTION_CANCEL.............");
break;
case MotionEvent.ACTION_UP:
Log.d("TAG", "ACTION_UP............."); btn.setBackgroundResource(R.drawable.microp_btn_normal);
break;
default:
break;
}
return false;
}
});

arg0.getParent().requestDisallowInterceptTouchEvent(true);//通知父控件勿拦截本控件touch事件

这样我们就和我们预期一样。

总结:

由于我的长按录制音频在ScrollView中,没有做onTouch冲突处理,所以出现非常多莫名其妙的问题。

如今要实现我们的要求,仅仅要在onLongClick中录制,ACTION_UP结束就可以。

demo下载:http://download.csdn.net/detail/msl0903/7224487

版权声明:本文博主原创文章。博客,未经同意不得转载。

Android onTouch、OnLongClick、onClick和ScrollView滑动事件冲突的更多相关文章

  1. Android 解决Gallery下ScrollView滑动事件冲突

    在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义 ...

  2. android 解决ListView点击与滑动事件冲突

    如果你的ListView的Item有滑动功能,但又点击Item跳转到其它activity,这样若是在Adapter里面写点击事件是会导致滑动事件获取不到焦点而失效: 解决方法:不要在adapter里面 ...

  3. 重写ListView解决ListView内部ViewPaper滑动事件冲突问题

    非常easy 重写ListView 其它类似问题解决ScrollView嵌套ViewPager出现的滑动冲突问题 http://blog.csdn.net/zhangyiacm/article/det ...

  4. 李氏滑动事件冲突解决方案 之 处理子ViewGroup的超棒方案

    父ViewGroup(CurView) 和 子 ViewGroup(ParentView) 滑动事件冲突解决方案 之 处理子ViewGroup的超棒方案: 子ViewGroup 以 SlipRelat ...

  5. Android ScrollView 嵌套 ListView、 ListView 嵌套ScrollView Scroll事件冲突解决办法

    本人菜鸟一名,最近工作了,开始学习Android. 最近在做项目的时候,UX给了个design,大概就是下拉刷新的ListView中嵌套了ScrollView,而且还要在ScrollView中添加动画 ...

  6. Android动画及滑动事件冲突解决(转载)

    原文链接:http://blog.csdn.net/singwhatiwanna/article/details/38168103 Android开发中动画和事件处理是程序员迈向高手的必经之路,也是重 ...

  7. webview滑动事件 与内部html左右滑动事件冲突问题的解决办法

    最近在做个混合app , 用html做页面,然后通过webview嵌套在activity中,效果是这样: 开始还是比较顺利,增加了菜单退出按钮,返回键页面回退功能,页面加载显示加载图标(在app端实现 ...

  8. SwipeRefreshLayout与ViewPager滑动事件冲突解决

    问题描写叙述: 开发中发现,SwipeRefreshLayout的下拉刷新,与ViewPager开发的banner的左右滑动事件有一点冲突,导致banner的左右滑动不够顺畅. 非常easy在bann ...

  9. Android Listview中Button按钮点击事件冲突解决办法

    今天做项目时,ListView中含有了Button组件,心里一早就知道肯定会有冲突,因为以前就遇到过,并解决过,可惜当时没有记录下来. 今天在做的时候,继续被这个问题郁闷了一把,后来解决后,赶紧来记录 ...

随机推荐

  1. 【免费】iPhone上最好用的短信群发软件: 高速短信4.1

    免费的最新的联系人.群组.多人发送短信软件短信群发4.1已经出炉.欢迎下载! *归属地信息让你时时记着好友的地方,让陌生号码变得不陌生:  *您能够选择最经常使用的联系人然后发送高速短信; *群联系人 ...

  2. Swift - 几种使用数组的数据存储模型

    在iOS游戏开发中,比如2048游戏.有时会需要存储N×N数组的数据模型(如3×3,4×4等).这里我们演示了三种实现方式,分别是:一维数组.仿二维数组.自定义二维数组(即矩阵结构). 功能是根据传入 ...

  3. Hibernate核心接口

    1.Configuration接口 Configuration负责管理Hibernate的配置信息. 2,SessionFactory接口 SessionFactory负责创建Session实例,能够 ...

  4. TApplication.Initialize的前世今生

    ---------------------------------------------------------------------------------------------------- ...

  5. 第二章 IoC Setter注入

    Setter注入又称为属性注入.是通过属性的setXXX()方法来注入Bean的属性值或依赖对象.由于Setter注入具有可选择性和灵活性高的优点,因此Setter注入是实际应用中最常用的注入方式. ...

  6. Spring核心技术

    这是第二次看关于Spring的资料,由于刚開始学习Spring的时候是边看视频边学习的,所以更注重的是实现代码,可是对宏观的掌握还是不够,这次主要从宏观的角度来分析一下Spring. 什么是Sprin ...

  7. JOHN W. TUKEY: HIS LIFE AND PROFESSIONAL CONTRIBUTIONS

    DAVID R. BRILLINGER 写的关于John的一片纪念文章 JOHN W. TUKEY: HIS LIFE AND PROFESSIONAL CONTRIBUTIONS  (The Ann ...

  8. 利用jquery+iframe做一个ajax上传效果

    以下是自学it网--中级班上课笔记 网址:www.zixue.it html页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict ...

  9. Android 表格布局<TableLayout>

    表格布局即,tableLayout,表格布局通过行.列的形式来管理UI组件,TablelLayout并不需要明确地声明包含多少行.多少列,而是通过TableRow,以及其他组件来控制表格的行数和列数, ...

  10. 14.3.5.1 An InnoDB Deadlock Example

    14.3.5 Deadlocks in InnoDB 14.3.5.1 An InnoDB Deadlock Example 14.3.5.2 Deadlock Detection and Rollb ...