2016-03-13 78 views
0

如何使用單擊按鈕從activity到android studio中的片段。請任何人告訴我什麼是確切的代碼意圖從活動到片段與按鈕點擊活動。如何在android studio中單擊按鈕從活動到片段

public class MainActivity extends ActionBarActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button mButNext = (Button) findViewById(R.id.btn_next); 
     mButNext.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Intent i = new Intent(this, PropertyListFragment.class); 
       startActivity(i); 
      } 
     }); 
    } 
} 

PropertyListFragment.class

public class PropertyListFragment extends Fragment { 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 
    public static PropertyListFragment newInstance(String param1, String param2) { 
     PropertyListFragment fragment = new PropertyListFragment(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

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

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     String strtext = getArguments().getString("pincode"); 
     return inflater.inflate(R.layout.fragment_property_list, container, false); 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (OnFragmentInteractionListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     public void onFragmentInteraction(Uri uri); 
    } 
} 

fragment_property_list.xml

<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"> 

    <FrameLayout 
     android:id="@+id/sample_content_fragment" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

回答

0

您使用的意圖,打開另一個活動。

如果您的目標是在自己的Activity中顯示Fragment,則創建一個啓動新Activity的意圖,並讓目標Activity將您的Fragment加載到onCreate中。見Adding a Fragment to an Activity

如果你的目標是展示在同一活動中的片段,然後讀取小幅以前的鏈接進一步下跌約程序的片段添加到現有的ViewGroup

1
Button mButNext = (Button) findViewById(R.id.btn_next); 
     mButNext.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       PropertyListFragment plf = PropertyListFragment.newInstance("param1", "param2"); 
getSupportFragmentManager().beginTransaction().replace(R.id.sample_content_fragment, plf, "tag").commit(); 
      } 
     }); 
相關問題