Java 如何处理片段中的 onClick

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35533905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:47:42  来源:igfitidea点击:

How to Handle onClick in Fragments

javaandroidxmlandroid-fragments

提问by Uday

This is my first try on Fragments and I'm not able to handle android:onClick

这是我第一次尝试 Fragments,但我无法处理 android:onClick

I have a button inside my fragment XML like this

我的片段 XML 中有一个像这样的按钮

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/save_keywords_button"
        android:id="@+id/save_keywords"
        android:layout_marginTop="340dp"
        android:background="#FF2E7D32"
        android:textColor="#FFFFFF"
        android:typeface="normal"
        android:onClick="myLogic" />

I searched many results and can't get the exact solution to handle the onClick event.

我搜索了很多结果,但无法获得处理 onClick 事件的确切解决方案。

My question is, How can I get the ID of my button and write the myLogic method. FindViewById() is not working in fragments and where should I write the method? in fragment or in my activity?

我的问题是,如何获取按钮的 ID 并编写 myLogic 方法。FindViewById() 在片段中不起作用,我应该在哪里编写该方法?在片段中还是在我的活动中?

采纳答案by parohy

Better approach would be implementing OnClickListenerto your fragment class and overriding onCreateViewin your fragment where you assign the listener to your button.

更好的方法是实现OnClickListener您的片段类并onCreateView在您将侦听器分配给按钮的片段中覆盖。

By putting onClickattribute in your XML layout, your activity on load will look for the element in the activity, not in the fragment. This will throw exception.

通过onClick在您的 XML 布局中放置属性,您的加载活动将在活动中查找元素,而不是在片段中。这将抛出异常。

I would suggest reading some fragment-activity hierarchy to understand when is it possible to access elements in your fragment.

我建议阅读一些片段活动层次结构以了解何时可以访问片段中的元素。

public class StartFragment extends Fragment implements OnClickListener{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_start, container, false);

    Button b = (Button) v.findViewById(R.id.save_keywords);
    b.setOnClickListener(this);
    return v;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.save_keywords:

        ...

        break;
    }
}
}

Reference from: here

参考资料:这里

回答by Nir Horovitz

Try to add method

尝试添加方法

public void myLogic(View v)

回答by GeekDroid

android:onClick="myLogic"will not call the method myLogicinside the fragment. Use OnClickListenerinstead to handle this type of events.

android:onClick="myLogic"不会调用myLogic片段内的方法。OnClickListener改为使用来处理此类事件。

See these below references

请参阅以下参考资料

Best way to implement View.OnClickListener in android

在 android 中实现 View.OnClickListener 的最佳方式

http://developer.android.com/reference/android/view/View.OnClickListener.html

http://developer.android.com/reference/android/view/View.OnClickListener.html

回答by callOfCode

If you need to get view in fragmen you can do this getView().findViewById(R.id.foo);only after onCreateView()has been called. And if you specify onClickin xml, you do not need to code any linking to that method in your program, just implement that method in your activity.

如果您需要在 Fragmen 中获取视图,则getView().findViewById(R.id.foo);只能在onCreateView()被调用后执行此操作。如果您onClick在 xml 中指定,则不需要在程序中编写任何链接到该方法的代码,只需在您的活动中实现该方法即可。