如何从运行时添加的片段中检索输入

时间:2016-08-15 21:01:36

标签: android android-fragments android-edittext android-fragmentactivity

Click to view issues on retrieving inputs from added fragments at run time

从上面的图片中,我只能从动态添加的第一个片段中检索第一个输入,但我无法检索之后添加的其他片段。请任何人可以帮助我如何实现这一点(从动态添加的几个片段中检索edittexts)?感谢。

以下是代码:

public class BasicDesign extends AppCompatActivity {

TextView ve;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.instance_shpw);

    Fragment fragment = new det();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.fragmentContainer, fragment, "Third");
    transaction.addToBackStack(null);
    transaction.commit();

}

public void DispText(View view) {

    //Works only for the first fragment
    //I would want it to work with other fragments when other fragments are added.

    TextView textview = (TextView) findViewById(R.id.DisplayText);
    EditText dt = (EditText) findViewById(R.id.Edt);
    String shw=dt.getText().toString();
    textview.setText(shw);

}

public void functionAdd(View view) {

    Fragment fragment = new det();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.fragmentContainer, fragment, "winter");
    transaction.addToBackStack("winter");
    transaction.commit();
}

public static class det extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dynamic_fragment, container, false);

    }
}
}

dynamic_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:background="#979797">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.8"
    android:padding="20dp"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    android:id="@+id/Edt" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+"
    android:padding="10dp"
    android:layout_weight="0.2"
    android:textStyle="bold"
    style="@style/Base.TextAppearance.AppCompat.Large"
    android:id="@+id/adder"
    android:onClick="functionAdd" />

</LinearLayout>

Instance_shpw.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/fragmentContainer"
    android:orientation="vertical">


</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Display Text"
        android:id="@+id/button4"
        android:layout_gravity="center_horizontal"
        android:onClick="DispText" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=""
        android:id="@+id/DisplayText"
        android:layout_gravity="center_horizontal"
        android:padding="16dp"/>


</LinearLayout>


</LinearLayout>

2 个答案:

答案 0 :(得分:1)

您应该从片段到父活动创建事件回调。请查看Android开发人员指南here

大致相同:

setDesktopMode(true)

并在父活动中:

public static class det extends Fragment {

     OnItemChangedListener mListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dynamic_fragment, container, false);
    }

    // Define the interface to be implemented by the activity.
    public interface OnViewChangedListener{
        public void onViewChanged(String value);
    }

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

    // Use the interface (i.e. call the method in the activity)
    // where you need it. For instance when your editText is changed:
    editText.addTextChangedListener(new TextWatcher() {
       public void afterTextChanged(Editable s) {
       }
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
       }
       public void onTextChanged(CharSequence s, int start, int before, int count) {
           mListener.onViewChanged(s);
       }
   });    
}

答案 1 :(得分:0)

您可以使用获取特定片段实例的viewid从运行时添加的片段中检索输入。在上面的代码中,它仅从第一个片段实例获取(EditText)findViewById(R.id.Edt)。

public class BasicDesign extends AppCompatActivity {
TextView ve;
private String recentFragmentTag;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.instance_shpw);

    Fragment fragment = new det();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.fragmentContainer, fragment, "Third");
    transaction.addToBackStack(null);
    transaction.commit();

    recentFragmentTag = "Third";
}

public void DispText(View view) {

    //Works only for the first fragment
    //I would want it to work with other fragments when other fragments are added.

    TextView textview = (TextView) findViewById(R.id.DisplayText);
    //EditText dt = (EditText) findViewById(R.id.Edt);
    Fragment recentFragment = getFragmentManager().findFragmentByTag(recentFragmentTag);
    EditText dt = (EditText)recentFragment.getView().findViewById(R.id.Edt);
    String shw=dt.getText().toString();
    textview.setText(shw);

}

public void functionAdd(View view) {

    Fragment fragment = new det();
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    transaction.add(R.id.fragmentContainer, fragment, "winter");
    transaction.addToBackStack("winter");
    transaction.commit();

    recentFragmentTag = "winter";
}

public static class det extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dynamic_fragment, container, false);

    }
  }
}