我有一个相对布局,其中有另一个相对布局,然后是一个滚动视图,它有另一个相对布局,其中包含几乎是列表类型布局中的按钮。它过去工作得很好,但我现在有一个问题。当我在我的设备上测试它并尝试滚动时,它不会滚动,但会初始化按钮上的ontouch,也不会滚动。这就好像按钮覆盖了滚动视图一样?我什么都试过了。当我把一个按钮放在滚动视图外的第一个相对视图中时,整个屏幕变成滚动视图中第一个按钮的outside侦听器。请帮帮忙?
就像我说的,我试着把相关的布局改成linears等,但都没有用
下面是我的类:
package com.pod3d.locatesa;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class Categories extends Activity implements OnTouchListener{
Button Button01,Button02,Button03,Button04,Button05,Button06,
Button07,Button08,Button09,Button10,Button11,Button12,home;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.categories);
Button01 = (Button) findViewById(R.id.Button01);
Button02 = (Button) findViewById(R.id.Button02);
Button03 = (Button) findViewById(R.id.Button03);
Button04 = (Button) findViewById(R.id.Button04);
Button05 = (Button) findViewById(R.id.Button05);
Button06 = (Button) findViewById(R.id.Button06);
Button07 = (Button) findViewById(R.id.Button07);
Button08 = (Button) findViewById(R.id.Button08);
Button09 = (Button) findViewById(R.id.Button09);
Button10 = (Button) findViewById(R.id.Button10);
Button11 = (Button) findViewById(R.id.Button11);
Button12 = (Button) findViewById(R.id.Button12);
Button01.setOnTouchListener(this);
Button02.setOnTouchListener(this);
Button03.setOnTouchListener(this);
Button04.setOnTouchListener(this);
Button05.setOnTouchListener(this);
Button06.setOnTouchListener(this);
Button07.setOnTouchListener(this);
Button08.setOnTouchListener(this);
Button09.setOnTouchListener(this);
Button10.setOnTouchListener(this);
Button11.setOnTouchListener(this);
Button12.setOnTouchListener(this);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
case R.id.Button01 :
Intent myIntent = new Intent(view.getContext(), Emergencies.class);
startActivity(myIntent);
break;
case R.id.Button02 :
Intent myIntent01 = new Intent(view.getContext(), TouristAttractions.class);
startActivity(myIntent01);
break;
case R.id.Button03 :
Intent myIntent02 = new Intent(view.getContext(), Accommodation.class);
startActivity(myIntent02);
break;
case R.id.Button04 :
Intent myIntent03 = new Intent(view.getContext(), Tourism_info.class);
startActivity(myIntent03);
break;
case R.id.Button05 :
Intent myIntent04 = new Intent(view.getContext(), Shopping.class);
startActivity(myIntent04);
break;
case R.id.Button06 :
Intent myIntent05 = new Intent(view.getContext(), Leisure.class);
startActivity(myIntent05);
break;
case R.id.Button07 :
Intent myIntent06 = new Intent(view.getContext(), Transport.class);
startActivity(myIntent06);
break;
case R.id.Button08 :
Intent myIntent07 = new Intent(view.getContext(), Fuel.class);
startActivity(myIntent07);
break;
case R.id.Button09 :
Intent myIntent08 = new Intent(view.getContext(), Events.class);
startActivity(myIntent08);
break;
case R.id.Button10 :
Intent myIntent09 = new Intent(view.getContext(), Banking.class);
startActivity(myIntent09);
break;
case R.id.Button11 :
Intent myIntent10 = new Intent(view.getContext(), Businesses.class);
startActivity(myIntent10);
break;
case R.id.Button12 :
Intent myIntent11 = new Intent(view.getContext(), Dining.class);
startActivity(myIntent11);
break;
}
return false;
}
}下面是我的布局xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/action" >
<Button
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="@drawable/home" android:text="@string/button_home"/>
</RelativeLayout>
<ScrollView
android:id="@+id/scrollview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/relativeLayout1" android:isScrollContainer="true">
<LinearLayout
android:id="@+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical"
android:orientation="vertical" >
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_emergency"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button02"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_tourist_int"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button03"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_accommodation"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button04"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_tousisminfo"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button05"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_shopping"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button06"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_leisure"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button07"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_transport"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button08"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_fuel"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button09"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_events"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button10"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_banking"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button11"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_businesses"
android:textColor="#cccccc"
android:textSize="20dp" />
<Button
android:id="@+id/Button12"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="@drawable/action"
android:text="@string/button_dining"
android:textColor="#cccccc"
android:textSize="20dp" />
</LinearLayout>
</ScrollView>
</RelativeLayout>下面是我的宣言:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="3"
android:versionName="3.5.1" package="com.pod3d.locatesa"
android:installLocation="preferExternal">
<!-- The application must be compiled using Google APIs (Android 3.0) -->
<!-- However, target and min SDK can be 8 (Android 2.2) -->
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.location.gps" android:required="false"/>
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="false"/>
<uses-feature android:name="android.hardware.sensor.compass" android:required="false"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<application
android:label="@string/app_name"
android:icon="@drawable/icon"
android:debuggable="true">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".Landing"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Categories"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Emergencies"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Dining"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Leisure"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Shopping"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".TourismInfo"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".TouristAttractions"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Transport"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Fuel"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Events"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Banking"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Businesses"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Hospitals"
android:screenOrientation="portrait"
>
</activity>
<activity android:name=".Fire"
android:screenOrientation="portrait"
></activity>
<activity android:name=".Police"
android:screenOrientation="portrait"
></activity>
<activity android:name=".Tourism_info"
android:screenOrientation="portrait"
></activity>
<activity android:name=".Animal"
android:screenOrientation="portrait"
></activity>
<!-- Start screen -->
<activity android:name=".SplashActivity"
android:theme="@style/Theme.Fullscreen"
android:screenOrientation="portrait"
android:label="@string/app_name">
</activity>
<!-- junaio AR view activity -->
<activity
android:name=".JunaioARViewTestActivity"
android:theme="@style/Theme.Fullscreen"
android:configChanges="orientation"
android:screenOrientation="landscape">
</activity>
<activity
android:name="com.metaio.junaio.plugin.view.POIDetailDialog"
android:theme="@style/Theme.POIDialog"
android:screenOrientation="landscape">
</activity>
<activity
android:name="com.metaio.junaio.plugin.view.WebViewActivity"
android:theme="@style/Theme.Fullscreen"
android:configChanges="orientation">"
</activity>
<activity
android:name="com.metaio.junaio.plugin.view.ImageViewActivity"
android:theme="@style/Theme.Fullscreen"
android:configChanges="orientation">
</activity>
</application>
</manifest>发布于 2012-04-02 17:21:24
我认为您正在尝试将onClick Listener附加到按钮上,但您这样做的方式是错误的,您已经实现了用于整个活动的onTouch方法,并且在onTouch()中用于case MotionEvent.ACTION_DOWN:以及一些其他操作,您正在执行一些应该从onClick方法调用的事件……
你应该在你的活动上实现View.onClickListener,
onClick(View v){
switch(v.getId()
{
case R.id.btnFoo:
// perform your action
break;
}
}为了将监听器附加到你的按钮,你应该使用Button.setOnClickListener(this);
发布于 2016-06-08 06:31:12
试试这个(注意ACTION_CANCEL,因为当scrollview在点击的按钮上滚动时会调用它):
@Override
public boolean onTouchEvent(MotionEvent event) {
if(isTouchable) {
int maskedAction = event.getActionMasked();
if (maskedAction == MotionEvent.ACTION_DOWN) {
this.setTextColor(resources.getColor(R.color.octane_orange));
initialClick = event.getX();
} else if (maskedAction == MotionEvent.ACTION_UP) {
this.setTextColor(defaultTextColor);
endingClick = event.getX();
checkIfSwipeOrClick(initialClick, endingClick, range);
} else if(maskedAction == MotionEvent.ACTION_CANCEL)
this.setTextColor(defaultTextColor);
}
return true;
}https://stackoverflow.com/questions/9973391
复制相似问题