setOnClickListener自动触发上一个片段

时间:2016-11-04 08:00:53

标签: android android-fragments onclicklistener

我有2个片段fragment_1和fragment_2,fragment_1调用fragment_2

当我点击选择对话框中的某个项目时,fragment_2会自动重定向到fragment_1  fragment_2代码

public class PersonalInfoFragment extends Fragment {
SharedPreferences sharedPreferences;
Context context;
// - - - - - - - - - -
String id, token, URL;
private FragmentTabHost tabHost;

//used for setting dates using calenders
Calendar myCalendar = Calendar.getInstance();
Calendar tempCal = Calendar.getInstance();


//declaring Strings to save the data of EditFields in it
String firstNameST, lastNameST, emailAddressST, mobileNumberST, passwordST,
        confirmPasswordST, dateOfBirthST, chooseStateST;

//declaring textFields
EditText  firstNameET, lastNameET, emailAddressET, mobileNumberET, dateOfBirthET, chooseStateET;

//declaring String and giving it a value used for the select travel mode buttons
String typeST = "-1";
ImageButton walkingButtonBT, bikeButtonBT, mobikeButtonBT, carButtonBT, pickupButtonBT;

//creating this string to load the states in it as a list
private String[] states_list = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut"
        , "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky"
        , "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri"
        , "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina"
        , "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"
        , "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};


public PersonalInfoFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().setTitle("Personal Info");

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

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    context = getActivity();



    sharedPreferences = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);

    firstNameET = (EditText) getActivity().findViewById(R.id.firstName);
    lastNameET = (EditText) getActivity().findViewById(R.id.lastName);
    emailAddressET = (EditText) getActivity().findViewById(R.id.emailAddress);
    mobileNumberET = (EditText) getActivity().findViewById(R.id.mobileNumber);

    carButtonBT = (ImageButton) getActivity().findViewById(R.id.carButton);
    carButtonBT.setOnClickListener(carButtonPressed);
    pickupButtonBT = (ImageButton) getActivity().findViewById(R.id.pickupButton);
    pickupButtonBT.setOnClickListener(pickupButtonPressed);

    //initialing setDate field
    dateOfBirthET = (EditText) getActivity().findViewById(R.id.dateOfBirth);

    //setting dateSetListener
    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabel();
            updateLabelToSave();
        }

    };
    //setting onClickListener on setDate
    dateOfBirthET.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            DatePickerDialog dpd = new DatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog, date,
                    myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)){
                @Override
                public void onCreate(Bundle savedInstanceState)
                {
                    super.onCreate(savedInstanceState);
                    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                }
            };


            //setting maxDate on tempCal
            long maxDate = new Date().getTime();
            tempCal.setTimeInMillis(maxDate);
            tempCal.set(Calendar.YEAR, tempCal.get(Calendar.YEAR) - 16);

            dpd.getDatePicker().setMaxDate(tempCal.getTimeInMillis());

            dpd.show();
        }
    });

    //initialing chooseState TextView
    chooseStateET = (EditText) getActivity().findViewById(R.id.chooseState);
    //setting inputType = null
    assert chooseStateET != null;
    chooseStateET.setInputType(InputType.TYPE_NULL);
    //creating ArrayAdapter and inserting state_list strings into it
    final ArrayAdapter<String> spinner_states = new ArrayAdapter<>(context,
            android.R.layout.select_dialog_item, states_list);
    //setting onClickListener
    chooseStateET.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            new AlertDialog.Builder(context)
                    .setTitle("Select State".toUpperCase())
                    .setAdapter(spinner_states, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            chooseStateET.setText(states_list[which]);
                            dialog.dismiss();
                        }
                    }).create().show();
        }
    });
}

//updating the setdate in the given format
private void updateLabel() {

    String myFormat = "MM/dd/yyyy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    dateOfBirthET.setText(sdf.format(myCalendar.getTime()));

}

//updating the setdate in the given format
private void updateLabelToSave() {

    String myFormat = "dd-MM-yyyy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    dateOfBirthST = sdf.format(myCalendar.getTime());

}

private View.OnClickListener carButtonPressed = new View.OnClickListener() {
    public void onClick(View v) {

        carButtonBT.setImageResource(R.drawable.car_selected);
        pickupButtonBT.setImageResource(R.drawable.pickup);
        typeST = "3";
    }
};

private View.OnClickListener pickupButtonPressed = new View.OnClickListener() {
    public void onClick(View v) {

        carButtonBT.setImageResource(R.drawable.car);
        pickupButtonBT.setImageResource(R.drawable.pickup_selected);
        typeST = "4";
    }
};

记录猫

11-04 15:00:45.802 6004-6058/com.bykar.bykar_provider E/Surface: getSlotFromBufferLocked: unknown buffer: 0x94ccd030
11-04 15:01:00.574 6004-6058/com.bykar.bykar_provider E/Surface: getSlotFromBufferLocked: unknown buffer: 0x94cccf50
11-04 15:01:00.578 6004-6004/com.bykar.bykar_provider W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.

0 个答案:

没有答案