用Button替换Fragment

时间:2015-05-07 19:03:00

标签: java android android-fragments

我正在尝试用按钮替换片段。基本上我有这个表单(这是helloFragment的所有输入视图),我想点击提交并显示下一个片段(showFormFragment)的表单信息。

它会在我的设备上呈现第一个片段,但该按钮不会替换它。但它并没有崩溃。

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/hello"
        android:name="teaminfamous.com.fragments.helloFragment"
        tools:layout="@layout/fragment_hello">
    </FrameLayout>
</RelativeLayout>

我的onCreate和Button列表:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction ft = fm.beginTransaction();

        helloFragment hf = new helloFragment();
        FormShow fs = new FormShow();
        ft.replace(android.R.id.content, hf);

        Button submitbtn = (Button) findViewById(R.id.submitButton);
        submitbtn.setOnClickListener(submitForm);

    }

编辑:

在Fragment类中,helloFragment:

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

    View view =  inflater.inflate(R.layout.fragment_form_show, container, false);
    Button submitbtn = (Button) view.findViewById(R.id.submitButton);
    submitbtn.setOnClickListener(submitForm);


    // Inflate the layout for this fragment
    return view;
}
private View.OnClickListener submitForm = new View.OnClickListener() {

    public void onClick(View v) {
        //do handling here
        final EditText nameField = (EditText) getView().findViewById(R.id.nameEdit);
        final EditText originField = (EditText) getView().findViewById(R.id.originEdit);
        String name = nameField.getText().toString();
        String origin = nameField.getText().toString();
        FragmentTransaction ft = fm.beginTransaction();
        FormShow show = new FormShow();
        ft.replace(R.id.hello, show, show.toString() ).addToBackStack(null).commit();

        //final TextView nameText = (TextView) findViewById(R.id.nameShow);
        // nameText.setText(name);
    }

};

我点击提交,onClickListner被调用,现在显示一个空白屏幕

我是Android的新手,也是Java的新手,所以对做这样的事情的通常模式的解释也会有所帮助。

2 个答案:

答案 0 :(得分:2)

为了动态替换片段,您必须在运行时添加它。要做到这一点,你需要一个容纳你的片段的容器,例如的FrameLayout:

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

然后您需要访问FragmentManager才能开始交易:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
String tag = fragment.getClass().getSimpleName();
transaction.replace( R.id.fragment_container, new YourFragmentHere(), tag);
transaction.commit();

答案 1 :(得分:1)

您应该尝试更改

  <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/hello"
        android:name="teaminfamous.com.fragments.helloFragment"
        tools:layout="@layout/fragment_hello">
  </fragment>

<FrameLayout> 然后,而不是这段代码:

ft.replace(android.R.id.content, hf);

使用

ft.replace(R.id.hello, hf).commit();

它应该有效