我的片段中的按钮无法识别

Posted

技术标签:

【中文标题】我的片段中的按钮无法识别【英文标题】:Button inside my Fragment is not recognised 【发布时间】:2020-01-08 16:29:22 【问题描述】:

我想测试该按钮是否适用于我的应用程序。 它们被放置在我的第一个片段中。当我单击“全部”按钮时,它根本没有响应。我的主要目标是加载片段,但在这种情况下,我放 System.out.println 只是为了测试我的按钮是否可以工作,但它显然不起作用。

我尝试制作新项目并且它可以工作,但仅在 Main Activity 中,我希望在我的 Experiments_Tab 片段中使用它。

我根本没有看到任何错误日志..

    public class Exercises_Tab extends Fragment 

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

                Button allbtn = (Button) Exercises.findViewById(R.id.all_button);

            allbtn.setOnClickListener(new View.OnClickListener() 
                @Override
                public void onClick(View v) 
                    System.out.println("Test");
                
            );
                    return Exercises;
                
            

还有我的 XML:

    <LinearLayout
        android:id="@+id/Linear_Layout"
        android:layout_
        android:layout_
        >
            <TextView
                android:layout_
                android:layout_
                android:text= " Muscles "
                android:background="#000000"
                android:textColor="#ffffff"
                android:textSize="25dp"

                android:layout_marginTop="3dp"/>






            <HorizontalScrollView
                android:id="@+id/scrollViewhorizontal"
                android:layout_
                android:layout_

                android:paddingVertical="-20dp">


                <LinearLayout
                    android:layout_
                    android:layout_
                    android:orientation="horizontal">




                    <Button
                        android:layout_
                        android:layout_
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"
                        android:background="#fafafa"
                        android:text="All"
                        android:textColor="#696969"
                        android:id="@+id/all_button"/>

                    <Button
                        android:layout_
                        android:layout_
                        android:text="Button 1"
                        android:id="@+id/second_button"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_
                        android:layout_
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"
                        android:background="#fafafa"
                        android:text="Button 1"
                        android:textColor="#696969" />
                    <Button
                        android:layout_
                        android:layout_
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_
                        android:layout_
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_
                        android:layout_
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_
                        android:layout_
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_
                        android:layout_
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_
                        android:layout_
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                    <Button
                        android:layout_
                        android:layout_
                        android:text="Button 1"
                        android:background="#fafafa"
                        android:textColor="#696969"
                        android:layout_margin="-20dp"
                        android:layout_marginRight="2dp"/>
                </LinearLayout>

            </HorizontalScrollView>

    </LinearLayout>




    <FrameLayout
        android:id="@+id/Frame_Layout"
        android:layout_
        android:layout_
        android:layout_gravity="center"
        android:layout_centerHorizontal="true"
        android:background="@color/colorPrimary"
        android:layout_below="@+id/Linear_Layout"


        >
    </FrameLayout>

【问题讨论】:

@peco 无论如何,您的答案与他们已经拥有的答案相同。最终,在膨胀的View 上调用findViewById() 的方法并不重要。 Button 是在膨胀期间创建的,而不是在 onCreateView() 返回之后创建的。 @Mike.M 因此,在 onViewCreate 或 onViewCreated 中设置 onClickListener 并不重要,因为布局是使用 inflater.inflate 创建的,并且在本例中位于 onCreateView 方法的第一行,对吗? @peco 对。 View 对象实际上是在 inflate() 调用中创建的,因此您可以在此之后随时修改它们。这些完全相同的对象稍后将被传递到onViewCreated(),但自onCreateView() 以来它们不会发生任何变化,因此如果你真的愿意,你可以在onCreateView() 中进行所有设置。基本上,onViewCreated() 方法只是让你的代码更容易保持整洁。 (顺便说一句,你并没有完全了解我的用户名,所以我没有收到通知。我并没有无视你,或任何事情。) @MikeM。我在我的新活动中尝试了类似的操作,并且效果很好,但是按钮、视图和充气器是在 MainActivity 中声明的,但在实际项目中,它们在 Fragment Class 中,而不是 Main Activity,我该怎么做才能修复我的问题,因为按钮不起作用。顺便说一句,感谢您的编辑。 很难说。 Fragment 代码很好,看起来这是正确的布局,因为否则它可能会崩溃。当您单击Button 时,您是否得到任何视觉反馈?也就是说,它看起来像被点击了吗?另外,您确定您不只是缺少该日志打印吗?你有没有尝试过一些更引人注目的东西,比如Toast?而且,你到底是如何加载Fragment 的?它是否在Activity 布局中的&lt;fragment&gt; 元素中?或者你在用代码做交易?你确定你在屏幕上看到的真的是Exercises_Tab吗? 【参考方案1】:

在片段的这个函数中执行你的整个代码,因为它已经引用了你的 xml 视图。

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) 
   super.onViewCreated(view, savedInstanceState);
   Button allbtn = (Button) view.findViewById(R.id.all_button);
   allbtn.setOnClickListener(new View.OnClickListener() 
       @Override
       public void onClick(View v) 
           System.out.println("Test");
           
        );
 

【讨论】:

【参考方案2】:

你可以简单地实现点击监听器

public class Exercises_Tab extends Fragment implements OnClickListener 
    Button allbtn ;

    @Override
    public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) 
        View Exercises = inflater.inflate(R.layout.fragment_1,container,false);
        allbtn = (Button) Exercises.findViewById(R.id.all_button);
        allbtn .setOnClickListener(this);
        return Exercises ;
    

    @Override
    public void onClick(View v) 
        // implements your things
    

我更喜欢这个,因为它使您的代码更具可读性。

【讨论】:

以上是关于我的片段中的按钮无法识别的主要内容,如果未能解决你的问题,请参考以下文章

更改无法识别我的 NSFetchedResultsController 附加到后台线程

UITableViewCell 在实现为 UITableView 的 FooterView 时无法识别按钮的操作,具有多个页脚视图

当 rvest 无法识别提交按钮时提交 POST 表单

监听器无法识别 ExpandableListView OnLongClick

在 ListFragment 中无法识别 findViewById [重复]

SwiftUI:自定义按钮无法识别具有清晰背景和 buttonStyle 的触摸