首页 > 解决方案 > 如何从包含在片段中的标题按钮开始活动?

问题描述

我实际上正在尝试从包含在片段中但无法启动的标题按钮打开一个活动。我是一个初学者,只是想探索事情是如何发生的。我已经搜索并没有找到任何答案希望有人可以回答。下面是我的代码。这是我的标题布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@color/lightOrange">

    <Button
        android:id="@+id/btnSignInUp"
        android:text="Signin / Signup"
        android:textAllCaps="false"
        android:textColor="@color/orange"
        android:layout_marginTop="55dp"
        android:textSize="20dp"
        android:layout_gravity="center"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:background="@drawable/round_button"/>
</LinearLayout>

这是我的片段

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  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"
    android:background="@color/white"
    tools:context=".AccountFragment">

    <include layout="@layout/header"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_marginTop="45dp"/>
</FrameLayout>

这是我的片段类

public class AccountFragment extends Fragment {

    Button btnSignInUp;

    public AccountFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_account, container, false);

        //binding id for signinup button
        this.btnSignInUp=view.findViewById(R.id.btnSignInUp);

        this.btnSignInUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(),SignInUpActivity.class));
            }
        });
        return view;
    }

}

实际上我正在从 BottomNavigationView 膨胀片段,并且从那里标题布局按钮假设打开一个新活动这是 BottomNavigationVIew 代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context=".MainActivity">

  <!--serve as a container for a different fragments-->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/white"
            app:itemIconTint="@drawable/bottom_navigation_selector"
            app:itemTextColor="@drawable/bottom_navigation_selector"
            app:menu="@menu/bottom_navigation_menu"
            app:labelVisibilityMode="labeled"/>
    </FrameLayout>


</LinearLayout>

当单击 btnSignInUp 时,我想打开一个名为 SignInUpActivity 的活动,不幸的是,我无法打开我在这里缺少的内容。指导将不胜感激。

标签: androidandroid-layoutandroid-fragmentsandroid-activity

解决方案


好的!!最后我解决了我的问题,问题是headerLayout被放置在 ScrollView 中,这阻止了我在按钮btnSignInUp上的onClickerListener。删除ScrollView后它起作用了。谢谢大家的支持。


推荐阅读