我有一个 LinearLayout,我想看看用户何时触摸它并松开手指。我想我可以通过添加一个 onTouch 侦听器并检查事件来做到这一点。这个 linearLayout 包含一堆按钮。
我的问题不是听众不工作,它只是没有做我认为它会做的事情。只有当布局被触摸时,监听器才会被激活,这是有道理的,但不是我想要的。如果用户触摸布局内的按钮,则不会调用布局侦听器,我希望调用它。
这是我的代码:
爪哇代码:
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 ,则使 main 方法返回 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
}
我认为这可行,但我无法测试它,因为没有用于扩展 LinearLayout 的 onFling 方法
有什么建议么?
请注意我返回的真/假可能是倒退的,我不确定这会让我感到困惑。但是,如果有人可以引导我制作 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 监听器工作得很好哈哈:|