有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java FloatingActionButton单击事件不工作

我在以下布局文件活动\注册表中有一个浮动操作按钮:

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

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

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

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

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

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

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

    </FrameLayout>


</RelativeLayout>

在活动中,我在浮动操作按钮上设置了单击事件。 我一直在调试和检查事件是否被触发,单击事件是否从未被触发。注册表活动代码如下所示:

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) 个答案

  1. # 1 楼答案

    @OnClick(R.id.take_photo) void openCameraIntent()
    {
        openCameraIntent1();
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        initialize();
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
    }
    
    private void initialize()
    {
        btnSavePhoto = findViewById(R.id.btn_save);
        photoView = findViewById(R.id.imageView);
        photoViewUnuse = findViewById(R.id.imageViewUnuse);
        previewPhotoText = findViewById(R.id.preview_text);
    
        btnSavePhoto.setOnClickListener(view -> submitDataToServer());
    
        ButterKnife.bind(this);
    }
    
  2. # 2 楼答案

    @OnClick注释的方法应该在onCreate之外

    @Override 
        public void onCreate(@Nullable Bundle savedInstanceState)
        { 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            ButterKnife.bind(this);
    
            //             TODO          -// 
            //     Set a click event for the floating action button 
    
    
        } 
    
     @OnClick({R.id.btn_Login, R.id.fb_Register}) 
            public void onClick(View view)
            { 
    
            }