我有一个LinearLayout,我想看看用户什么时候碰过它,并释放了他们的手指。我想我可以通过添加一个onTouch侦听器并检查事件来完成这个任务。这个linearLayout有一堆按钮。
我的问题不是听者不工作,而是没有做我想做的事情。只有当布局被触摸时,侦听器才会被激活,这是有意义的,但不是我想要的。如果用户触摸布局中的一个按钮,布局侦听器就不会被调用,我希望调用它。
这是我的代码:
java代码:
ll = (LinearLayout)findViewById(R.id.CategorySelect_lLayout_noID);
ll.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(CategorySelect.this, "action DOWN called", Toast.LENGTH_SHORT).show();
return true;
}
if(event.getAction() == MotionEvent.ACTION_UP){
Toast.makeText(CategorySelect.this, "action Up called", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});我的xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:scrollbars="none"
>
<LinearLayout
android:id="@+id/CategorySelect_lLayout_noID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/CategorySelect_b_1"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_1"
/>
<Button
android:id="@+id/CategorySelect_b_2"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_2" />
<Button
android:id="@+id/CategorySelect_b_3"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_3" />
<Button
android:id="@+id/CategorySelect_b_4"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_4" />
<Button
android:id="@+id/CategorySelect_b_5"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_5" />
<Button
android:id="@+id/CategorySelect_b_6"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_6" />
<Button
android:id="@+id/CategorySelect_b_7"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_7" />
</LinearLayout>
</ScrollView>感谢您的阅读。
更新的评论代码:
我的主要xml文件是从我的活动中膨胀出来的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/categoryselect_background"
android:scaleType="fitXY"
>
<TextView
android:id="@+id/CategorySelect_tv_chooseacat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:gravity="center"
android:text="@string/CategorySelect_tv_chooseacat"
android:textColor="#FF5500"
android:textSize="34dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/CategorySelect_b_settings"
android:background="@drawable/menu_settings"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
/>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:scrollbars="none"
>
<com.tdubstudios.soundboard.hungergames.ScrollAbleLinearLayout
android:id="@+id/CategorySelect_lLayout_noID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/CategorySelect_b_cato"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_1"
/>
<Button
android:id="@+id/CategorySelect_b_cinna"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_2" />
<Button
android:id="@+id/CategorySelect_b_effie"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_3" />
<Button
android:id="@+id/CategorySelect_b_gale"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_4" />
<Button
android:id="@+id/CategorySelect_b_haymitch"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_5" />
<Button
android:id="@+id/CategorySelect_b_katniss"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_6" />
<Button
android:id="@+id/CategorySelect_b_peeta"
style="@style/CustomBLUEButtonTextStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/CategorySelect_b_7" />
</com.tdubstudios.soundboard.hungergames.ScrollAbleLinearLayout>
</ScrollView>
</RelativeLayout>这是ScrollAbleLinearLayout类:
package com.tdubstudios.soundboard.hungergames;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
public class ScrollAbleLinearLayout extends LinearLayout {
public ScrollAbleLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
return false; // Tell children not to use this event
}
}是的,我知道一些东西的命名已经从我以前的代码中改变了,那是因为我改变了它,以隐藏我正在制作的应用程序类型(从搜索结果)。然而,无论出于什么原因,我不再关心,这就是背后的理由。
谢谢。
第三版:
所以我现在用
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
//requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
if(event.getAction() == MotionEvent.ACTION_DOWN){
return false;
}
if(event.getAction() == MotionEvent.ACTION_UP){
return false;
}
return true; // Tell children not to use this event
}它的工作方式,我希望它,但这是不切实际的。要真正注册按钮单击,您必须确保您的手指没有移动的所有左/右/上/下。它必须是精确的下降和准确的向上。我试图添加一个onFling方法,然后跟踪移动手指的距离。如果移动小于x像素量,那么主方法返回false,这将允许按钮工作,但如果它实际上是一个滑动/移动,那么按钮不需要被激活。我的意思是:
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(e1.getY() - e2.getY()) > MORE_THAN_X_INT) {
booleanVar = false;
return false;
}else{
booleanVar = true;
}
return false;
}然后做:
Override
public boolean onInterceptTouchEvent(MotionEvent event) {
//requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
if(event.getAction() == MotionEvent.ACTION_DOWN){
return false;
}
if(event.getAction() == MotionEvent.ACTION_UP){
return false;
}
return booleanVar; // Tell children not to use this event
}我认为这是可行的,但我不能测试它,因为没有用于扩展onFling的LinearLayout方法
有什么建议吗?
请注意,我返回的真假可能是向后的,我不确定这是不是有点让我困惑。但是,如果有人能引导我做那个onFling方法,我就可以把它弄乱,得到正确的值。
我有过
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
//requestDisallowInterceptTouchEvent(true); // Tell parents not to use this event
while(event.getAction() == MotionEvent.ACTION_DOWN){
return true;
}
while(event.getAction() == MotionEvent.ACTION_UP){
return true;
}
return false; // Tell children not to use this event
}我的按钮永远不会点击。但是ontouch侦听器工作得很好哈哈:
发布于 2012-06-16 01:17:28
我建议扩展LinearLayout并使用onInterceptTouchEvent捕捉触摸事件,然后返回true以强制其子级不接收事件。
就像这样:
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return true; // Tell children not to use this event
}
}然后,在XML中,将LinearLayout更改为com.yourpackagename.MyLinearLayout。
增编:如果希望Button的和 LinearLayout都接收事件,可以将onInterceptTouchEvent更改为:
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Whatever touch event code you want, using the "event" variable
return false; // Allow children to use the event
}发布于 2012-06-16 02:52:31
所以我从来没有真正找到我的问题的答案,但我找到了一个可行的方法。我只是为整个活动添加onTouchEvent方法,而不是一个特定的视图,而不是将侦听器放在布局上。
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(CategorySelect.this, "action DOWN called", Toast.LENGTH_SHORT).show();
}
if(event.getAction() == MotionEvent.ACTION_UP){
Toast.makeText(CategorySelect.this, "action Up called", Toast.LENGTH_SHORT).show();
}
return super.onTouchEvent(event);
}它不会影响任何按钮,所以这正是我想要的。谢谢你和我一起工作。
https://stackoverflow.com/questions/11059940
复制相似问题