Java 浮动操作按钮单击事件不工作
我在以下布局文件活动\注册表中有一个浮动操作按钮:Java 浮动操作按钮单击事件不工作,java,android,xml,Java,Android,Xml,我在以下布局文件活动\注册表中有一个浮动操作按钮: <?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/t
<?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>
用
@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)
{
}
我有我的
@Onclick
注释方法,用于在onCreate()外部浮动ActionButton。。但它不起作用@Bind
注释起作用,但对于我添加ButterKnife.Bind(这个)的@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)
{
}
@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);
}