java - 带有 onTouch 的 Android Scrollview 和 RelativeLayout 按钮

标签 java android xml android-layout layout

我有一个相对布局,其中包含另一个相关布局,然后是一个 ScrollView ,其中包含另一个包含几乎列表类型布局中的按钮的相对布局。它过去工作正常,但我现在遇到了问题。当我在我的设备上测试它并尝试滚动时,它不会滚动但会初始化按钮上的 ontouch 并且不会滚动。好像按钮覆盖了 ScrollView ?我已经尝试了一切。当我在 ScrollView 外的第一个相对 View 中放置一个按钮时,整个屏幕变为 ScrollView 中第一个按钮的 ontouchlistener。请帮忙?

就像我说的,我已经尝试将相对布局更改为线性等,但无济于事

这是我的类(class):

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>

这是我的 list :

<?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>

最佳答案

我认为您正在尝试在按钮上附加 onClick 监听器,但是您以错误的方式执行此操作,您已经实现了 onTouch 方法,该方法适用于您的整个 Activity 并在 onTouch() 如果 MotionEvent.ACTION_DOWN: 以及您正在执行的一些其他操作应该从 onClick 方法调用...

你应该在你的 Activity 上实现 View.onClickListener 并实现,

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

     }

}

并且为了将 Listener 附加到您的按钮,您应该 Button.setOnClickListener(this);

关于java - 带有 onTouch 的 Android Scrollview 和 RelativeLayout 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9973391/

相关文章:

xml - 解析 XML 文件中的特定标签

java - "System resource exceeded"在通过 Java jdbc odbc 连接到 Access 文件期间

android - 使用 gradle 或在 android studio 中运行测试会导致 NoClassDefFoundError

c# - XmlDocument.SelectSingleNode 使用哪个版本的 XPath?

java - Gradle:com.android.support:support-compat:25.3.1 和 com.android.support:animated-vector-drawable:24.0.0 必须使用完全相同的版本

java - 对android中自定义对象的数组列表进行排序

python - 添加 "encoding="utf-8"?> 到 xml 文件

java - 编写快速 CRUD 应用程序的最快方法。当我有一些可重用的 Java 代码时?

java - Stripe : Add default card and do not override old card

java - Tomcat 服务器无法通过 Servlet 连接到 MongoDB