获取不同片段的EditText值

时间:2014-02-25 21:55:38

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

我试图从editTexts获取一些关于不同片段的文本。所以我首先要做的是定义我的mPagermPagerAdapter

a_Atenuacion活动

public class a_Atenuacion extends FragmentActivity{

    private static final int NUM_PAGES = 3;
    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.a_dat_viewpager);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);


    }
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int position) {
            switch(position){
                case 0:
                    return new a_Dat_Inicio1();
                case 1:
                    return new a_Dat_Inicio2();
                case 2:
                    return new a_Dat_Inicio3();
            }
            return null;
        }
        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}

然后我得到了我的3个片段类(第1和第2个布局都有editText,但第3个布局有editTextbutton)。按钮功能是当我在最后一个片段(fragment3)时,它采用信息形式(不同的editTexts)并发送到另一个活动。

a_Dat_Inicio1片段

public class a_Dat_Inicio1 extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

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

        return view;  
    }
}

a_Dat_Inicio3片段

public class a_Dat_Inicio3 extends Fragment {

    EditText edit3;
    EditText edit2;
    EditText edit1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("Test", "hello");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.a_dat_inicio3, container, false);


        edit1 = (EditText)getActivity().findViewById(R.id.editText1);
        final String edit11 = edit1.getText().toString();
        edit2 = (EditText)getActivity().findViewById(R.id.editText2);
        final String edit22 = edit2.getText().toString();
        edit3 = (EditText)view.findViewById(R.id.editText3);
        final String edit33 = edit3.getText().toString();

        Button but=(Button) view.findViewById(R.id.button);

        but.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {

                //Creamos el bundle
                Bundle bundle = new Bundle();
                //Le ponemos la variable parametro con el contenido (key, valor)
                bundle.putString("edit3", edit33);
                bundle.putString("edit2", edit22);
                bundle.putString("edit1", edit11);
                Intent net= new Intent(v.getContext(),Prueba1.class);
                net.putExtras(bundle);
                startActivity(net);

            }
        });

        return view;
    }
}

最后我在另一个活动(Prueba1.class)上得到了包,很奇怪我只得到editText1的结果(在第一个片段上),其余的都是空的。

有人可以帮我吗?

提前致谢。

2 个答案:

答案 0 :(得分:1)

最后我克服了一个界面,这是我认为在已经定义的片段上得到soemthing的唯一方法。

我的代码是这样的:

首先定义一个接口

public interface OnEditTextChanged {

public void onEditPressed1(String edit1);

public void onEditPressed2(String edit1);

public void onEditPressed3(String edit1);

然后片段活动:

public class a_Dat_Inicio1 extends Fragment {

EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.e("Test", "hello");
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.a_1dat_inicio1, container, false);
    init (view);

    return view;
}

OnEditTextChanged editListener;
@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    try{
        editListener=(OnEditTextChanged) getActivity();
    }catch(ClassCastException e){
        throw new ClassCastException(activity.toString()+"must implemnt onEditPressed");
    }
}

private void init(View view) {
    edit=(EditText) view.findViewById(R.id.editText1);

    //cada vez que se modifique texto llamar
    edit.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            final String edit11 = edit.getText().toString();
            editListener.onEditPressed1(edit11);

        }
    });

最后在我们的主要活动中,调用方法:

public class a_Atenuacion extends FragmentActivity implements OnEditTextChanged {

String dat1;
String dat2;
String dat3;

private static final int NUM_PAGES = 3;
private PagerAdapter mPagerAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.a_1dat_viewpager);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);


    //HERE DO WHATEVER YOU WANT WITH THE DATA CAUGTH ON THE EDIT 1 METHOD


}


@Override
public void onEditPressed1(String edit1) {

    if(mPager.getCurrentItem()==0){
        dat1=edit1;
        Toast bread = Toast.makeText(getApplicationContext(), "edit1", Toast.LENGTH_LONG);
        bread.show();
    }
}
希望有人帮助!!!

以任何方式谢谢!!!

答案 1 :(得分:0)

试试这个:

在你的第二个片段中,传递intent extras和你的值。例如:

在你的第二个片段中,执行以下操作:

Intent someintent = new Intent();
        //Some first result
        someintent.putExtra("your_value_one", your_value_one);
        //Some Second result
        someintent.putExtra("your_value_two", your_value_two);
        //Some third result
        someintent.putExtra("your_value_three", your_value_three);
        getActivity().setResult(getActivity().RESULT_OK,someintent);
        getActivity().finish();

在您想要此结果的其他片段中,请在您的其他片段上执行此操作:

1)制定一些获取信息的方法。

private void getInfo() {

                    //Note: The values are coming from a diff activity or fragment and                 so the strings should match.
                    Intent data = new Intent();
            String first_value = data.getStringExtra("your_value_one");
                    String second_value = data.getStringExtra("your_value_two");
                    String third_value = data.getStringExtra("your_value_three");
            Log.i("First Value: ", first_value);
                    Log.i("First Value: ", second_value);
                    Log.i("First Value: ", third_value);

        }

制作此方法后,只需在onActivityCreated()上调用它。

现在您可以随时随地使用这些字符串。如果要在其他任何地方使用这些值,请确保在一开始就定义字符串,以便可以在任何地方使用这些值。

希望这个答案有所帮助.. :)