2012-04-02 81 views
2

我有一個相對佈局與其他relatve laout在其中,然後與其他相對佈局的滾動視圖幾乎包含列表類型佈局中的按鈕。它使用工作正常,但我現在有一個問題。當我在我的設備上測試它時,我嘗試滾動它不滾動,但初始化按鈕上的ontouch並不滾動。它就像按鈕覆蓋滾動視圖一樣?我已經嘗試了一切。當我在滾動視圖之外的第一個相對視圖中放置一個按鈕時,整個屏幕變成滾動視圖中第一個按鈕的ontouchlistener。請幫忙?Android滾動視圖和RelativeLayout按鈕與onTouch

就像我說的,我試圖改變相對佈局線性電源等,都無濟於事

這裏是我的類:

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> 

回答

0

我想你試圖在按鈕上附加onClick監聽器,但是你以這種方式做錯了,你已經實現了onTouch方法,這個方法適用於你的整個活動,在onTouch()的情況下MotionEvent.ACTION_DOWN:和其他一些你正在執行的動作這應該從onClick方法調用事件...

,你應該在你的活動實施View.onClickListener和實施,

onClick(View v){ 
    switch(v.getId() 
    { 
    case R.id.btnFoo: 
    // perform your action 
    break; 

    } 

} 

以及用於連接監聽器的按鈕,你應該Button.setOnClickListener(this);

+0

非常感謝,現在檢查出來 – user1285471 2012-04-02 09:50:36

0

試試這個(注意ACTION_CANCEL,因爲這就是所謂當在點擊的按鈕上方滾動視圖滾動):

@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; 
}