8

我有一个 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();

我得到空值。帮帮我..提前谢谢。

4

6 回答 6

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;
        });

返回视图朋友,然后使用默认布局充气器......

于 2013-01-24T09:02:20.913 回答
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 方法,这更明显。

于 2013-01-24T09:50:00.743 回答
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;
}
于 2016-01-21T02:43:27.550 回答
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;
    }
}
于 2013-01-24T09:02:13.550 回答
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;
}
}

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

于 2013-03-08T14:44:14.997 回答
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;
}
于 2015-09-25T11:22:04.440 回答