gpt4 book ai didi

java - FloatingActionButton 单击事件不起作用

转载 作者:行者123 更新时间:2023-11-30 10:32:19 26 4
gpt4 key购买 nike

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

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

在 Activity 中,我在 float 操作按钮上设置了一个 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();
}

最佳答案

@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)
{

}

关于java - FloatingActionButton 单击事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42749015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com