验证不适用于我的应用程序

时间:2014-05-12 07:45:41

标签: android validation

我有两个按钮tab1和tab2.Within tab1我有很少的edittext。我想只有当tab1中的所有edittext都填满时才允许用户点击tab2,它们不应该是空的。

package com.megasys.roushfunway.survey;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.megasys.roushfunway.MasterFragment;
import com.megasys.roushfunway.MasterFragmentActivity;
import com.megasys.roushfunway.R;

public class SurveyFragment extends MasterFragment implements OnClickListener {

    View view;
    String[] seventPublic, syes, time, people,usa;
    Button tab1,tab2;
    LinearLayout layout1,layout2;

    ArrayAdapter<String> eventyes;
    ArrayAdapter<String> usaStates;
    ArrayAdapter<String> eventTime;
    ArrayAdapter<String> eventPublic;
    ArrayAdapter<String> eventPeople;
    boolean check=false;

    EditText etNoEventDays=null, etMaterialQtyHanded, etEventItemType, etFeedback,
            etCommentOpportunity, etHotelName, etHotelCity, etHotelZip,
            etHotelRate, etHotelAddress, etHotelPhone;
    Spinner spEventPublicPrivate, spEventContact, spPassMaterial,
            spAddedEventItems, spEventTimeFrame, spPeopleAttached,
            spIntroduceOnsiteContact, spState, spWasHotelBooked;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();

    }

    private void initView() {

        etNoEventDays = (EditText) view.findViewById(R.id.et_no_of_events);
        etMaterialQtyHanded = (EditText) view
                .findViewById(R.id.et_material_quantity_handed);
        etEventItemType = (EditText) view
                .findViewById(R.id.et_event_items_type);
        etFeedback = (EditText) view.findViewById(R.id.et_feedback);
        etCommentOpportunity = (EditText) view
                .findViewById(R.id.et_opportunity);
        etHotelName = (EditText) view.findViewById(R.id.et_Hotel_Name);
        etHotelCity = (EditText) view.findViewById(R.id.et_Hotel_City);
        etHotelAddress = (EditText) view.findViewById(R.id.et_Hotel_Address);
        etHotelZip = (EditText) view.findViewById(R.id.et_Hotel_Zip);
        etHotelRate = (EditText) view.findViewById(R.id.et_Hotel_Rate);
        spAddedEventItems = (Spinner) view
                .findViewById(R.id.sp_event_added_items);
        spEventPublicPrivate = (Spinner) view
                .findViewById(R.id.sp_event_public_private);
        spEventContact = (Spinner) view
                .findViewById(R.id.sp_event_contact_prepared);
        spEventTimeFrame = (Spinner) view
                .findViewById(R.id.sp_event_time_frame);
        spPassMaterial = (Spinner) view.findViewById(R.id.sp_pass_out_material);
        spPeopleAttached = (Spinner) view
                .findViewById(R.id.sp_introduce_yourself);
        spPeopleAttached = (Spinner) view
                .findViewById(R.id.sp_no_of_people_attended);
        spState = (Spinner) view.findViewById(R.id.sp_state);
        spIntroduceOnsiteContact = (Spinner) view
                .findViewById(R.id.sp_introduce_yourself);
        spWasHotelBooked = (Spinner) view.findViewById(R.id.sp_hotel_booked);

        seventPublic = getResources().getStringArray(R.array.public_private);
        eventPublic = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_spinner_item, seventPublic);
        eventPublic
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spEventPublicPrivate.setAdapter(eventPublic);

        syes = getResources().getStringArray(R.array.yes_no);
        eventyes = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_spinner_item, syes);
        eventPublic
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spEventContact.setAdapter(eventyes);
        spAddedEventItems.setAdapter(eventyes);
        spPassMaterial.setAdapter(eventyes);
        spIntroduceOnsiteContact.setAdapter(eventyes);
        spWasHotelBooked.setAdapter(eventyes);

        time = getResources().getStringArray(R.array.time_frame);
        eventTime = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_spinner_item, time);
        eventTime
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spEventTimeFrame.setAdapter(eventTime);

        people = getResources().getStringArray(R.array.people_attended);
        eventPeople = new ArrayAdapter<String>(mContext,
                android.R.layout.simple_spinner_item, people);
        eventPeople
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spPeopleAttached.setAdapter(eventPeople);

        usa=getResources().getStringArray(R.array.USA);
        usaStates=new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item,usa);
        usaStates
        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spState.setAdapter(usaStates);
tab1=(Button) view.findViewById(R.id.tab1);
tab2=(Button) view.findViewById(R.id.tab2);
tab1.setOnClickListener(this);
tab2.setOnClickListener(this);
layout1=(LinearLayout) view.findViewById(R.id.layout1);
layout2=(LinearLayout) view.findViewById(R.id.layout2);

    }

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

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()) {
        case R.id.tab1:
        layout1.setVisibility(arg0.VISIBLE);
        layout2.setVisibility(arg0.GONE);



            break;
            case R.id.tab2:
                validation();
                if(check){
                layout2.setVisibility(arg0.VISIBLE);
                layout1.setVisibility(arg0.GONE);
                }else{
                    layout1.setVisibility(arg0.VISIBLE);
                    layout2.setVisibility(arg0.GONE);
                }


        default:
            break;
        }

    }

    public boolean validation(){

        if(etNoEventDays==null){
            etNoEventDays.setBackgroundColor(Color.RED);
            MasterFragmentActivity.showprogressDialog(mContext, "Error",
                    "Please fill all the mandatory fields", true);
            check=true;
            }
        return check;





    }

}

我有两个按钮tab1和tab2.Within tab1我有很少的edittext。我想只有当tab1中的所有edittext都填满时才允许用户点击tab2,它们不应该是空的。

0 个答案:

没有答案