按下浮动操作按钮后,活动也会启动

时间:2016-10-15 14:14:13

标签: android floating-action-button

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/relativeView"
    android:background="@color/white"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/today_date"
        android:orientation="vertical" >    

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"

            android:orientation="horizontal" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginEnd="@dimen/activity_vertical_margin"
                android:id="@+id/ll1"
                android:layout_width="match_parent"
                 android:layout_height="match_parent"
                android:background="@drawable/rectangle"
                android:layout_weight="1.0"
                />   

            <LinearLayout
                android:orientation="horizontal"
                android:layout_marginBottom="@dimen/activity_vertical_margin"
                android:layout_marginEnd="@dimen/activity_vertical_margin"
                android:id="@+id/ll2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/rectangle"
                android:layout_weight="1.0"
                />    
         </LinearLayout>

    <LinearLayout
        android:weightSum="2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:orientation="horizontal" >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginEnd="@dimen/activity_vertical_margin"
            android:id="@+id/ll3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/rectangle"
            android:layout_weight="1.0"
             />

        <LinearLayout
            android:orientation="horizontal"
            android:layout_marginBottom="@dimen/activity_vertical_margin"
            android:layout_marginEnd="@dimen/activity_vertical_margin"
            android:id="@+id/ll4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/rectangle"
            android:layout_weight="1.0"
             >    
        </LinearLayout>    
    </LinearLayout>    
</LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_add_reminders"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_plus"
        app:backgroundTint="#ffffff"
        android:layout_gravity="bottom|end" />

</android.support.design.widget.CoordinatorLayout>

因此,每当我按下fab时,背景线性布局的附加活动也会打开。那么有谁能告诉我代码有什么问题?即使把FAB按钮放在线性布局之外,我也不知道为什么会出现这个问题。

Java代码: -

fab=(FloatingActionButton) findViewById(R.id.fab_add_reminders);

        fab.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                //Set reminder dialog box appear
                openDialogToAddReminder();
                return false;
            }
        });


nightLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Constants.SelectedBucket="Night";
                startActivity(new Intent(Reminders.this, ListReminders.class));
            }
        });

1 个答案:

答案 0 :(得分:0)

您在OnTouchListener而不是FloatingActionButton上使用OnClickListener,而您在OnTouchListener内也返回false,这意味着fab不会消耗触摸事件并将其传递到下面的下一个视图(nightLL)。

将您的代码更改为:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openDialogToAddReminder();
        }
    });

或者,如果您出于任何其他原因想要使用OnTouchListener,请使用此代码:

fab.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Toast.makeText(FabClickActivity.this, "FAB Touch.", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });