通过以编程方式在fragment中添加edittext获取Value

时间:2016-08-09 12:16:41

标签: android android-fragments

我有片段。在该片段中,我以编程方式创建edittext小部件,如下所示:

driverItem = new EditText(getContext());
driverItemArray.add(driverItem);
driverItem.setId((int) dataSnapshot.getChildrenCount());
driverItem.setHint(people.getItem());
driverItem.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
lin.addView(driverItem);

那么当我按 MainActivity 中的按钮时,如何从我的片段中获取edittext值?

使用新代码更新

  

这是我的主要活动

public class MainActivity extends AppCompatActivity implements DriverFragment.OnFragmentInteractionListener{

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout tabLayout;

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


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setOffscreenPageLimit(3);

     setupViewPager(mViewPager);//mViewPager.setAdapter(mSectionsPagerAdapter);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    setupTabIcons();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            //        .setAction("Action", null).show();


    //BUTTON FIRES AND GET THE VALues FROM FRAGMENT
            onReturnValue(s);

        }
    });
}


private void setupTabIcons(){
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(new DriverFragment(), "");
    adapter.addFragment(new ErrorFragment(), "");
    adapter.addFragment(new InfoFragment(), "");
    viewPager.setAdapter(adapter);
}

@Override
public void onReturnValue(String driverItem) {
    System.out.println(driverItem);
}
  

这是我的片段

public class DriverFragment extends Fragment{
ListView listView;

List<String> driverItemArray = new ArrayList<String>();

private OnFragmentInteractionListener mListener;

public DriverFragment(){

}

public interface OnFragmentInteractionListener {
    void onReturnValue(String driverItem);

}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Firebase.setAndroidContext(getActivity());
}

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

    View view = inflater.inflate(R.layout.fragment_driver,container,false);
    listView = (ListView) view.findViewById(R.id.listViewDriver);

return view;

}

@Override
public void onStart(){
    super.onStart();
   FirebaseListAdapter<String> adapter = new FirebaseListAdapter<String>(
            getActivity(),
            String.class,
            R.layout.textview_layout,
            mRootRef) {
        @Override
        protected void populateView(View view, String s, int i) {
            TextInputLayout textInputLayout = (TextInputLayout)view.findViewById(R.id.input_layout);
            EditText editText = (EditText)view.findViewById(R.id.input_edittext);
            editText.setId(i);
            textInputLayout.setHint(s);
            driverItemArray.add(s);
        }
    };
    listView.setAdapter(adapter);
    Toast.makeText(getActivity().getApplicationContext(), listView.getAdapter().getCount()+"", Toast.LENGTH_SHORT).show();
}

}

3 个答案:

答案 0 :(得分:0)

尝试此操作将保存String数组中的所有EditText文本

String[] strings = new String[](driverItemArray.length);
for(int i=0; i < driverItemArray.length; i++){
    string[i] = driverItemArray[i].getText().toString();
}

答案 1 :(得分:0)

执行此操作的方法是使用回调模式。因此,当您创建片段时,它接受一个接口。调用该按钮时,它会调用该接口上的方法。然后,活动可以是实现接收事件的接口的活动。 Android Studio甚至可以为您创建样板。

片段

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

按钮上单击侦听器

onReturnValue(driverItem.getText().toString())

界面本身

public interface OnFragmentInteractionListener {         void onReturnValue(String driverItem);     }

活动

public class MyActivity implements OnFragmentInteractionListener  {
    ....

    public void onReturnValue(String value) {
       //youre inside the activity now so use the value
    }
}

答案 2 :(得分:-1)

在你的片段中,将driverItem声明为全局变量,并使用此方法:

public String getEditTextValue() {
 return driverItem.getText().toString().trim();
}

从活动中你有一个片段的实例。然后调用上面的方法:

String value = fragmentInstance.getEditTextValue();