如何向按钮“ toClick”中的片段添加按钮

问题描述 投票:0回答:1

我想在用户单击按钮时显示Toast消息。

我尝试了很多解决方案,但对我不起作用。

这里是代码

Java “ AccountFragment”

public class AccountFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_account, container, false);

    // return the view "Layout"
    return rootView;
}

// display message for the user
public void Tosta (View view) {
    Toast.makeText(getActivity() , "There is no need to Login because you are a tester :D" , Toast.LENGTH_LONG).show();
}}

XML“ fragment_account”我用过onClick

<Button
    android:id="@+id/button_login_google"
    android:layout_width="261dp"
    android:layout_height="50dp"
    android:layout_marginTop="50dp"
    android:background="@drawable/account_view2"
    android:text="Login with Google"
    android:onClick="Tosta"
    android:textColor="#fff"
    android:textSize="18dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView_login" />
java android xml android-fragments toast
1个回答
0
投票

[签出:

public class AccountFragment extends Fragment {

Button btn;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_account, container, false);

    // return the view "Layout"
    return rootView;
}

// display message for the user
@Override
public void onClick(View v) {
    // Toast here
}

您也可以尝试一下:

btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                      //toast here

                    }
                });
© www.soinside.com 2019 - 2024. All rights reserved.