简体   繁体   English

android的新手,如何为片段内的元素初始化事件处理程序

[英]New to android, how do I initialize event handlers for elements inside a fragment

(Disclaimer: pretty new to android) So I am trying to make a tabbed application using Android Studio, and following examples online I have been able to use the actionbar to setup 2 tabs and fragments like so: (免责声明:android的新手)因此,我尝试使用Android Studio创建一个选项卡式应用程序,并且在网上以下示例中,我已经能够使用操作栏来设置2个选项卡和片段,如下所示:

in MainActivity.java MainActivity.java中

    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setTitle("My App Test");

    Tab tabSearch = bar.newTab().setText("Search").setIcon(android.R.drawable.ic_menu_search);
    Tab tabReport = bar.newTab().setText("Report").setIcon(android.R.drawable.ic_menu_report_image);

    Fragment stfrag = new SearchTabFragment();
    Fragment rptfrag = new ReportFragment();

    tabSearch.setTabListener(new MyTabsListener(stfrag));
    tabReport.setTabListener(new MyTabsListener(rptfrag));


    bar.addTab(tabSearch);
    bar.addTab(tabReport);

in ReportFragment/SearchTabFragment .java ReportFragment / SearchTabFragment .java中

public class SearchTabFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){

         return inflater.inflate(R.layout.fragment_1,container,false);

    }
 }

I basically just copied the example for the ActionBar listener directly from the tutorial I found: 我基本上只是直接从发现的教程中复制了ActionBar侦听器的示例:

class MyTabsListener implements ActionBar.TabListener {
    public Fragment fragment;

    public MyTabsListener(Fragment fragment){
        this.fragment = fragment;
    };

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft){
        ft.replace(R.id.wrap,fragment);
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft){

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft){

    }


}

Now my question is, how do I attach eventHandlers to the elements defined in my fragments.xml? 现在的问题是,如何将eventHandlers附加到fragments.xml中定义的元素上? Previously in a "Single Activity" application I played with I just put it in the main java file right after the onCreate event. 以前,在我玩过的“单一活动”应用程序中,我只是将它放在onCreate事件之后的主Java文件中。 Eg: 例如:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    (TextView) tv = (TextView) findViewByid(R.id.textviewidhere);
    //now I attach the handlers as I need

Thanks in advance! 提前致谢! (edited for clarity) (为清楚起见进行了编辑)

You should probably read a little bit about fragments . 您可能应该阅读一些有关片段的信息 You need to override onViewCreated and "catch" views there: 您需要在其中覆盖onViewCreated和“ catch”视图:

 public class MyFragment extends Fragment {
    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.my_fragment, container,
                false);
        return rootView;

    }

//and you use rootView to call findViewById
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Button myButton = (Button) rootView.findViewById(R.id.mybutton);
        //or you can set some other listener, or "catch" some different view -checkbox,          //textview etc
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do something you want
            }
        });
    }

Example : 范例:

public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){

     View root = inflater.inflate(R.layout.fragment_1,container,false);
     TextView text = (TextView)root.findViewById(R.id.mybutton);

    return root;
 }

} }

this will let use the TextView inside the fragment. 这样就可以在片段内使用TextView了。

Modifed code lifted straight from the docs : 修改后的代码直接来自docs

Scenario: 场景:

Let us say you have a Fragment with a button inside it. 假设您有一个Fragment ,里面有一个按钮。 You would like to attach the activity as the event listener to that button inside fragment. 您想将活动作为事件侦听器附加到片段中的该按钮。 Then, 然后,

public static class ButtonFragment extends Fragment implements OnClickListener {

    private OnClickListener mListener;

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

        // inflate the corresponding fragment XML
        View v = inflater.inflate(R.layout.example_fragment, container, false);

        // grab the button and attach this fragment as its listener
        ((Button)v.findViewById(R.id.btn)).setOnClickListener(this);

        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {

            // once this fragment is attached to its activity, check to see
            // if it implements OnClickListener
            mListener = (OnClickListener) activity;

        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " doesn't implement OnClickListener");
        }
    }

    ...
    public void onClick(View v) {
        if(mListener){
            // once the button inside fragment is clicked,
            // notify the activity about the same
            mListener.onClickListener(v);
        }
    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android - 如何从活动中“开始”或“初始化”片段? - Android - How do I “start” or “initialize” a Fragment from an activity? 如何将事件处理程序添加到列表视图复选框单元格中的复选框? - How do i add event handlers to checkboxes in a listview checkbox cell? Android:在片段内切换到新片段? - Android: switch to a new fragment inside a fragment? Android:如何初始化View和Canvas? - Android: How do I initialize View and Canvas? 如何在Vert.x中对事件处理程序进行单元测试(来自事件总线)? - How do I unit test event handlers (from the event bus) in Vert.x? Android-如何使用相同的片段来编辑和创建模型的新实例? - Android - How do I use the same fragment for editing and creating a new instance of my model? 如何在Android的Fragment中的onClick侦听器中使对话框工作? - How do I make a Dialog Box work inside an onClick Listener in a Fragment in Android? 在Java中,如何在几行中而不是一行中初始化数组中的元素? - In Java how do I initialize elements in array in several lines, not one? 如何在UML中表示一个Android片段? - How do I represent an android fragment in UML? 在Java中使用SAX事件处理程序,如何在不同事件之间保留变量? - Using SAX event handlers in Java, How do I keep variables across different events?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM