如何访问viewpager中片段内的edittext值

时间:2013-01-24 08:56:11

标签: android android-viewpager fragment

我有一个mainActivity,它由一个视图分页器中的3个片段活动组成。每个片段由编辑文本组成。如何在水平滑动片段时获取编辑文本的值?我想这样做。下面的代码

public class MainActivity extends FragmentActivity  {

int value = 0;
Context context;
String a = "";

SectionsPagerAdapter mSectionsPagerAdapter;


ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.activity_main);

    // Set up the action bar.
    //final ActionBar actionBar = getActionBar();
//  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mAddButtonB = (Button) findViewById(R.id.addnewB);

    mAddSearchV = findViewById(R.id.addnewSearch);
    mAddButtonB.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            mAddSearchV.setVisibility(View.GONE);
            mSectionsPagerAdapter = new SectionsPagerAdapter(
                    getSupportFragmentManager());
            mViewPager = (ViewPager) findViewById(R.id.customviewpager);
            mViewPager.setAdapter(mSectionsPagerAdapter);


        }
    });
        }



public class SectionsPagerAdapter extends FragmentPagerAdapter {

    PageZero p0;
    PageOne p1;
    PageTwo p2;

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
        p0 = new PageZero();
        p1 = new PageOne();
        p2 = new PageTwo();
    }

    @Override
    public Fragment getItem(int position) {


        switch (position) {
        case 0:
             return p0;
        case 1:
            return p1;
        case 2:
            return p2;
        }
        return null;
    }


    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }


}
// PageZero = (Fragment1)getFragmentManager().findFragmentByTag("frag1");
public class PageZero extends Fragment {
 private  EditText name;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.componentname, container,
                false);
        name=(EditText)view.findViewById(R.id.nameTV);

        name.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                a=name.getText().toString().trim();
                Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();


            }
        });/*(new OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                a=name.getText().toString().trim();
                Toast.makeText(getApplicationContext(), a,Toast.LENGTH_SHORT).show();
                return false;
            }
        });*/
        return view;
    }
}

public class PageOne extends Fragment {

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

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


        return view;
    }
}

public class PageTwo extends Fragment {

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

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


}

当我尝试访问pageZero类中的eddittext时,如下所示

 name=(EditText)view.findViewById(R.id.nameTV);
 a=name.getText().toString().trim();

我的价值是空的。请帮帮我......谢谢。

6 个答案:

答案 0 :(得分:5)

更改pagezero类oncreateview代码,就像这样......

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view =getActivity().getLayoutInflater().inflate(R.layout.componentname,null);
        name=(EditText)view.findViewById(R.id.nameTV);

        name.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                a=name.getText().toString().trim();
                Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();


            }

 return view;
        });

返回查看朋友,然后使用默认布局inflater ...

答案 1 :(得分:3)

当您点击edittext字段时,是否有任何内容?也许你应该在edittext字段的丢失焦点事件中获取文本。因此,您可以先输入一些文字......

示例:

name.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus == false){
                //get text from edittext field...
            }

        }
    });

您尝试获取文字的方式对我来说是正确的。至少我是这样做的。

在PagerAdapter的'getItem'方法中,您应该直接实例化您的片段:

@Override
public Fragment getItem(int position) {     
    switch (position) {
        case 0:
            return new PageZero();
        case 1:
            return new PageOne();
        case 2:
            return new PageTwo();
    }
    return null;
}

如果想要创建新项目,Viewpager只会调用getItem。不幸的是,他们调用方法getItem而不是createItem,这更明显。

答案 2 :(得分:3)

EditText置于onClick阻止内...

这意味着更改代码:

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

        name=(EditText)view.findViewById(R.id.nameTV);

        name.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                  a=name.getText().toString().trim();
                  Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
             }
        });

    return view;
}

要:

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

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

                  // GET EDIT TEXT HEAR:
                  EditText name=(EditText)view.findViewById(R.id.nameTV);

                  a=name.getText().toString().trim();
                  Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();
             }
        });

    return view;
}

答案 3 :(得分:1)

要使用视图寻呼机创建3个不同的片段,您应该:

1)使用视图寻呼机和视图寻呼机适配器创建FragmentActivity。

public class FragmentActivity extends FragmentActivity
{
    private FragmentAdapter adapter;
    private ViewPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);     
        adapter = new FragmentAdapter(getSupportFragmentManager());
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(adapter);
   }

2)使用静态实例

创建3个片段
public final class Fragment1 extends Fragment
{   
    public static Fragment1 newInstance() {
        return new Fragment1();
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout layout = new LinearLayout(getActivity());
// your fragment xml view
        return view;
    }
}

3)用它填充视图寻呼适配器;

public class FragmentAdapter extends FragmentPagerAdapter

{    
    public InstallFragmentAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {     
        switch (position) {
            case 0:
                return Fragment1.newInstance();
            case 1:
                return Fragment2.newInstance();
            case 2:
                return Fragment3.newInstance();
        }
        return null;
    }
}

答案 4 :(得分:0)

public class PageZero extends Fragment {
   public static  EditText name;   //change this to public static

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


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.componentname, container,
            false);
    name = (EditText)view.findViewById(R.id.nameTV);

    name.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
        }
    });

    return view;
}
}

然后,您可以从另一个像这样的片段访问公共静态EditText名称(当然是在生成它的视图之后)。

public class PageOne extends Fragment {

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

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

   EditText page_one_text = PageZero.name;   //you can get that variable in any fragment now, but using the class name of that fragment to access it

    return view;
}
}

干杯。如果有帮助,请将此标记为正确答案。你在这里有很多答案

答案 5 :(得分:0)

我试过下面的代码。为我工作

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


    View v =  inflater.inflate(R.layout.fragment_main_explore, container, false);
    ed1 = (EditText)v.findViewById(R.id.editTextTemp);
    if( ed1 == null )
    {
        Toast.makeText(getActivity(), "ERROR UNABLE TO GET EDIT", Toast.LENGTH_LONG).show();
    }


    // Inflate the layout for this fragment
    //return inflater.inflate(R.layout.fragment_main_explore, container, false);
    return v;
}