Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.jens.myapplication.booking; //from w w w . ja v a 2 s . com import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Toast; import com.example.jens.myapplication.R; import com.example.jens.myapplication.domain.Person; import com.example.jens.myapplication.domain.binding.PersonValidatorBinding; import com.example.jens.myapplication.domain.binding.ValidatorBinding; import com.example.jens.myapplication.domain.validator.PersonValidator; import com.example.jens.myapplication.sam.MyFragment; import com.example.jens.myapplication.sam.MySaveableFragment; import com.example.jens.myapplication.util.BindingUtils; import com.example.jens.myapplication.view.MyLinearLayout; import java.util.LinkedList; import java.util.List; public class BookingBondFragment extends MyFragment implements MySaveableFragment { public static final String ARG_PERSON = "person"; public static final String ARG_USER_INFO = "userinfo"; private OnFragmentInteractionListener mListener; private MyLinearLayout mLlyRoot; private Button mBtnNext; private Button mBtnBack; private CheckBox mChkAangesloten; private boolean mInitChecked = false; private boolean mDisableCheckbox = false; private EditText mTxtAansluitingsnr; private EditText mTxtCodeGerechtigde; private Person mPerson; private PersonValidator mPersonValidator; private List<PersonValidatorBinding> mBindings; public static BookingBondFragment newInstance(){ return newInstance(null); } /** * * @param bundle The starting bundle (received from the fragment before), null if u dont have any * @return */ public static BookingBondFragment newInstance(Bundle bundle) { BookingBondFragment fragment = new BookingBondFragment(); if(bundle != null){ fragment.setArguments(bundle); } return fragment; } public BookingBondFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(getArguments() != null){ Bundle bundle = getArguments(); if(bundle.containsKey(ARG_USER_INFO)){ mPerson = bundle.getParcelable(ARG_USER_INFO); //Automatically check if the person currently has Bond information. mInitChecked = mPerson.hasBondInformation(); if(mInitChecked){ mDisableCheckbox = true; } } else{ mPerson = bundle.getParcelable(ARG_PERSON); if(mPerson != null){ mInitChecked = true; } } } if(mPerson == null){ mPerson = new Person(); } mPersonValidator = new PersonValidator(mPerson); mBindings = new LinkedList<PersonValidatorBinding>(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_booking_bond, container, false); mLlyRoot = (MyLinearLayout) rootView.findViewById(R.id.llyRootLinearLayout); mLlyRoot.setOnDispatchTouchEventListener(new MyLinearLayout.OnDispatchTouchEventListener() { @Override public void onDispatchTouchEvent(MotionEvent e) { clearFocus(); } }); mTxtAansluitingsnr = (EditText) rootView.findViewById(R.id.txtBondAansluitingsnr); mTxtCodeGerechtigde = (EditText) rootView.findViewById(R.id.txtBondCodeGerechtigde); mChkAangesloten = (CheckBox) rootView.findViewById(R.id.chkIsBijBond); mChkAangesloten.setChecked(mInitChecked); if(mDisableCheckbox){ mChkAangesloten.setEnabled(false); } mChkAangesloten.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { changeChecked(isChecked); } }); mBtnBack = (Button) rootView.findViewById(R.id.btnBack); mBtnNext = (Button) rootView.findViewById(R.id.btnNext); mBtnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { goBack(); } }); mBtnNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { goNext(); } }); initValidators(); changeChecked(isAangesloten()); validateAll(); return rootView; } /** * Clear the focus of the fields */ private void clearFocus(){ View view = getActivity().getCurrentFocus(); if(view == null || view == mLlyRoot){ return; } mLlyRoot.requestFocus(); } /** * Called when the checked value of the checkbox is checked * @param checked */ private void changeChecked(boolean checked){ mTxtAansluitingsnr.setText(mPerson.getBondAansluitingsNr()); mTxtCodeGerechtigde.setText(mPerson.getBondCodeGerechtigde()); // boolean required = checked && !mDisableCheckbox; for(ValidatorBinding binding : mBindings){ // binding.setRequired(required); binding.setRequired(checked); // binding.getView().setEnabled(required); binding.getView().setEnabled(checked); if(!checked){ binding.getView().setText(""); } } validateAll(); } /** * validate all the text fields */ private void validateAll(){ /*if(!isAangesloten()){ return; }*/ for(ValidatorBinding binding : mBindings){ binding.validate(); } } /** * * @return true if the checkbox is checked, false if unchecked */ private boolean isAangesloten(){ return mChkAangesloten.isChecked(); } /** * Initialize the bindings for the textfields */ private void initValidators(){ mBindings.add(new PersonValidatorBinding(mPersonValidator, mTxtAansluitingsnr, PersonValidatorBinding.FIELD_BOND_AANSLUITINGSNUMMER, mTxtAansluitingsnr.getHint().toString())); mBindings.add(new PersonValidatorBinding(mPersonValidator, mTxtCodeGerechtigde, PersonValidatorBinding.FIELD_BOND_CODE_GERECHTIGDE, mTxtCodeGerechtigde.getHint().toString())); } /** * Tell the listener to go to the previous screen */ private void goBack(){ mListener.toContactFragment(); } /** * Tell the listener to go to the next screen if validation allows it */ private void goNext(){ validateAll(); for(ValidatorBinding binding : mBindings){ if(binding.getView().getError() != null){ Toast.makeText(getActivity(), R.string.error_errors_exist, Toast.LENGTH_SHORT).show(); return; } } mListener.onFinishBond(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); passBundleToParent(); mListener = null; } @Override public void passBundleToParent() { if(isAangesloten()){ Bundle bundle = new Bundle(); bundle.putParcelable(ARG_PERSON, mPerson); mListener.passBondBundle(bundle); } else{ mListener.passBondBundle(new Bundle()); } } public interface OnFragmentInteractionListener { /** * Bond info is finished, go to the next screen */ public void onFinishBond(); /** * Go to the contact page */ public void toContactFragment(); /** * Save the bundle with bond information * @param bundle */ public void passBondBundle(Bundle bundle); } }