簡體   English   中英

android的新手,如何為片段內的元素初始化事件處理程序

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

(免責聲明:android的新手)因此,我嘗試使用Android Studio創建一個選項卡式應用程序,並且在網上以下示例中,我已經能夠使用操作欄來設置2個選項卡和片段,如下所示:

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);

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);

    }
 }

我基本上只是直接從發現的教程中復制了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){

    }


}

現在的問題是,如何將eventHandlers附加到fragments.xml中定義的元素上? 以前,在我玩過的“單一活動”應用程序中,我只是將它放在onCreate事件之后的主Java文件中。 例如:

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

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

提前致謝! (為清楚起見進行了編輯)

您可能應該閱讀一些有關片段的信息 您需要在其中覆蓋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
            }
        });
    }

范例:

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;
 }

}

這樣就可以在片段內使用TextView了。

修改后的代碼直接來自docs

場景:

假設您有一個Fragment ,里面有一個按鈕。 您想將活動作為事件偵聽器附加到片段中的該按鈕。 然后,

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM