单击“浮动操作”按钮的事件不起作用

时间:2017-03-12 18:30:00

标签: java android floating-action-button

我在以下布局文件activity_register中有一个浮动操作按钮:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:paddingTop="70dp"
        android:layout_width="320dp"
        android:layout_height="400dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

        <android.support.v7.widget.CardView
            android:id="@+id/cv_RegisterAdd"
            app:cardBackgroundColor="#009688"
            android:layout_marginTop="10dp"
            android:layout_gravity="center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="320dp"
            app:cardCornerRadius="6dp"
            app:cardElevation="3dp"
            app:cardUseCompatPadding="true"
            >
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_marginTop="50dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="50dp"
                    android:text="REGISTER"
                    android:textColor="#FFFFFF"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textStyle="bold"
                    />
                <LinearLayout
                    android:layout_marginTop="10dp"
                    android:paddingStart="50dp"
                    android:paddingEnd="30dp"
                    android:layout_width="match_parent"
                    android:layout_height="40dp">
                    <android.support.design.widget.TextInputLayout
                        android:textColorHint="#f0f7f4"
                        android:layout_width="match_parent"
                        android:theme="@style/TextLabel"
                        android:layout_height="wrap_content">
                        <EditText
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:hint="Username"
                            android:textColor="#f0f7f4"
                            android:id="@+id/txtCurUsername"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:inputType="textPersonName"
                            android:background="@drawable/selector_bg_edit_2"
                            android:textCursorDrawable="@drawable/bg_input_cursor_2"
                            android:paddingBottom="2dp"
                            />
                    </android.support.design.widget.TextInputLayout>
                </LinearLayout>
                <LinearLayout
                    android:paddingStart="50dp"
                    android:paddingEnd="30dp"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="40dp">
                    <android.support.design.widget.TextInputLayout

                        android:textColorHint="#FFFFFF"
                        android:theme="@style/TextLabel"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <EditText
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:hint="Password"
                            android:textColor="#f0f7f4"
                            android:id="@+id/txtNewPassword"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:inputType="textPassword"
                            android:background="@drawable/selector_bg_edit_2"
                            android:textCursorDrawable="@drawable/bg_input_cursor_2"
                            android:paddingBottom="2dp"
                            />
                    </android.support.design.widget.TextInputLayout>
                </LinearLayout>
                <LinearLayout
                    android:paddingStart="50dp"
                    android:paddingEnd="30dp"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="40dp">
                    <android.support.design.widget.TextInputLayout

                        android:textColorHint="#f0f7f4"
                        android:theme="@style/TextLabel"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <EditText
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:textColor="#f0f7f4"
                            android:hint="Repeat Password"
                            android:id="@+id/txtRepeatPassword"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:inputType="textPassword"
                            android:background="@drawable/selector_bg_edit_2"
                            android:textCursorDrawable="@drawable/bg_input_cursor_2"
                            android:paddingBottom="2dp"
                            />
                    </android.support.design.widget.TextInputLayout>
                </LinearLayout>
                <Button
                    android:layout_marginTop="20dp"
                    android:layout_gravity="center_horizontal"
                    android:stateListAnimator="@drawable/state_list_animator_z"
                    android:id="@+id/btn_Register"
                    android:text="REGISTER"
                    android:textColor="#009688"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_width="200dp"
                    android:layout_height="40dp"
                    android:background="@drawable/register_btnshape"
                    >
                </Button>

            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fb_Cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:fabSize="normal"
            android:src="@drawable/plus_x"
            android:transitionName="loginFab"
            android:layout_gravity="center_horizontal|top" />

    </FrameLayout>   
</RelativeLayout>

在活动中,我在浮动操作按钮上设置了Click事件。我一直在调试和检查事件是否被触发,并且从未触发click事件。 RegisterActivity代码如下:

public class RegisterActivity extends AppCompatActivity
{

    @BindView(R.id.fb_Cancel)
    FloatingActionButton mClose_fb;
    @BindView(R.id.btn_Register)
    Button mRegister_btn;
    @BindView(R.id.cv_RegisterAdd)
    CardView mRegister_cv;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        getSupportActionBar().hide();

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            showEnterAnimation();
        }
        mClose_fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                animationRevealClose();
            }
        });
    }


    public void showEnterAnimation()
    {
        Transition transition = TransitionInflater.from(this).inflateTransition(R.transition.fabtransition);
        getWindow().setSharedElementEnterTransition(transition);

        transition.addListener(new Transition.TransitionListener() {
            @Override
            public void onTransitionStart(Transition transition) {
                mRegister_cv.setVisibility(View.GONE);
            }

            @Override
            public void onTransitionEnd(Transition transition) {
                transition.removeListener(this);
                animateRevealShow();
            }

            @Override
            public void onTransitionCancel(Transition transition) {

            }

            @Override
            public void onTransitionPause(Transition transition) {

            }

            @Override
            public void onTransitionResume(Transition transition) {

            }
        });
    }

    public void animationRevealClose()
    {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(mRegister_cv,mRegister_cv.getWidth()/2,0, mRegister_cv.getHeight(), mClose_fb.getWidth() / 2);
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                mRegister_cv.setVisibility(View.INVISIBLE);
                super.onAnimationEnd(animation);
                mClose_fb.setImageResource(R.drawable.plus);
                RegisterActivity.super.onBackPressed();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
    }

    public void animateRevealShow()
    {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(mRegister_cv, mRegister_cv.getWidth()/2,0, mClose_fb.getWidth() / 2, mRegister_cv.getHeight());
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationStart(Animator animation)
            {
                mRegister_cv.setVisibility(View.VISIBLE);
                super.onAnimationStart(animation);
            }
        });
        mAnimator.start();
    }

    @Override
    public void onBackPressed() {
        animationRevealClose();
    }

2 个答案:

答案 0 :(得分:0)

在你的onCreate中执行此操作:

Setup

答案 1 :(得分:0)

没有动画发生的原因是因为我遗漏了 mAnimator.start()在以下方法中启动动画

 public void animationRevealClose()
    {
        Animator mAnimator = ViewAnimationUtils.createCircularReveal(mRegister_cv,mRegister_cv.getWidth()/2,0, mRegister_cv.getHeight(), mClose_fb.getWidth() / 2);
        mAnimator.setDuration(500);
        mAnimator.setInterpolator(new AccelerateInterpolator());
        mAnimator.addListener(new AnimatorListenerAdapter()
        {
            @Override
            public void onAnimationEnd(Animator animation)
            {
                mRegister_cv.setVisibility(View.INVISIBLE);
                super.onAnimationEnd(animation);
                mClose_fb.setImageResource(R.drawable.plus);
                RegisterActivity.super.onBackPressed();
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
     mAnimator.start();
    }