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

前端之家收集整理的这篇文章主要介绍了android – 如何访问viewpager中片段内的edittext的值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个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(),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,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.componentaddress,false);


        return view;
    }
}

public class PageTwo extends Fragment {

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

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


}

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

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

我得到了null值.请帮帮我..谢谢你.

解决方法

像这样更改pagezero类oncreateview代码…..
@Override
    public View onCreateView(LayoutInflater inflater,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(),Toast.LENGTH_SHORT).show();


            }

 return view;
        });

返回视图朋友,然后使用默认布局inflater …

原文链接:https://www.f2er.com/android/316330.html

猜你在找的Android相关文章