从视图中检索用户输入的片段数据

时间:2016-05-08 03:49:53

标签: android android-fragments

我正在从frag1移动到frag2,在frag1中我有2个编辑文本的名字和姓氏。在进入frag2之前我也在验证。但无论在编辑文本中输入数据,它都会显示两个编辑文本的值。这是代码......

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.signup_1st_new,container,false);
    fname = (EditText) rootView.findViewById(R.id.si_firstname);
    lname = (EditText) rootView.findViewById(R.id.si_lastname);
    firstName = fname.getText().toString();
    lastName = lname.getText().toString();

    next1 = (TextView) rootView.findViewById(R.id.next1);

    next1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(firstName.isEmpty() || lastName.isEmpty()){
                Toast.makeText(getActivity().getApplicationContext(), "no name", Toast.LENGTH_LONG).show();
            }
            else {

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, secondFragment);
                fragmentTransaction.commit();
            }
        }
    });
    return rootView;
}

2 个答案:

答案 0 :(得分:0)

首先在'onClick()'方法中,你应该将数据设置为'firstName'和'lastName',如

'first-name = fName.getText()。toString()'和姓氏相同

答案 1 :(得分:0)

 next1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Add these two lines on your onClick
            firstName = fname.getText().toString();
            lastName = lname.getText().toString();

            if(firstName.isEmpty() || lastName.isEmpty()){
                Toast.makeText(getActivity().getApplicationContext(), "no name", Toast.LENGTH_LONG).show();
            }
            else {

                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_container, secondFragment);
                fragmentTransaction.commit();
            }
        }
    });