簡體   English   中英

浮動操作按鈕的點擊事件不起作用

[英]Click event of Floating action button not working

我在以下布局文件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();
    }

在您的onCreate中執行以下操作:

mClose_fb = (FloatingActionButton)findViewById(R.id.fb_Cancel);
mClose_fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                animationRevealClose();
            }
        });

沒有動畫發生的原因是因為我在以下方法中省略了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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM