Android 验证在我的应用程序中不起作用

Android 验证在我的应用程序中不起作用,android,validation,Android,Validation,我有两个按钮tab1和tab2。在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 and

我有两个按钮tab1和tab2。在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;





    }

}
package com.megasys.roushfunway.survey;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.LinearLayout;
导入android.widget.Spinner;
导入com.megasys.roushfunway.MasterFragment;
导入com.megasys.rushfunway.MasterFragmentActivity;
导入com.megasys.roushfunway.R;
公共类SurveyFragment扩展了主片段实现OnClickListener{
视图;
字符串[]seventPublic,syes,time,people,usa;
按钮tab1、tab2;
线性布局布局1、布局2;
ArrayAdapter事件是;
ArrayAdapter美国;
ArrayAdapter事件时间;
ArrayAdapterEventPublic;
ArrayAdapterEventPeople;
布尔检查=假;
EditText etNoEventDays=null,etMaterialQtyHanded,etEventItemType,etFeedback,
etCommentOpportunity、etHotelName、etHotelCity、etHotelZip、,
etHotelRate、etHotelAddress、etHotelPhone;
Spinner spEventPublicPrivate、spEventContact、spPassMaterial、,
spAddedEventItems、spEventTimeFrame、SppeopleaAttached、,
spIntroduceOnsiteContact、spState、spWasHotelBooked;
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
initView();
}
私有void initView(){
etNoEventDays=(EditText)view.findViewById(R.id.et\u事件的编号);
etMaterialQtyHanded=(编辑文本)视图
.findViewById(R.id.et\材料\数量\已交付);
etEventItemType=(编辑文本)视图
.findViewById(R.id.et\u事件\u项目\u类型);
etFeedback=(EditText)view.findViewById(R.id.et_feedback);
etCommentOpportunity=(编辑文本)视图
.findviewbyd(R.id.et_机遇);
ethodelname=(EditText)view.findViewById(R.id.et_Hotel_Name);
ethodelcity=(EditText)view.findViewById(R.id.et_Hotel_City);
ethodeladdress=(EditText)view.findViewById(R.id.et_Hotel_Address);
ethodelzip=(EditText)view.findViewById(R.id.et_Hotel_Zip);
ethodelrate=(EditText)view.findViewById(R.id.et_Hotel_Rate);
spAddedEventItems=(微调器)视图
.findViewById(R.id.sp\u事件\u添加的\u项目);
spEventPublicPrivate=(微调器)视图
.findViewById(R.id.sp\u事件\u公共\u私人);
spEventContact=(微调器)视图
.findViewById(准备R.id.sp事件和联系人);
spEventTimeFrame=(微调器)视图
.findViewById(R.id.sp\u事件\u时间帧);
spPassMaterial=(微调器)view.findViewById(R.id.sp\u pass\u out\u material);
sppeopleaAttached=(微调器)视图
.findviewbyd(R.id.sp_自我介绍);
sppeopleaAttached=(微调器)视图
.findViewById(R.id.sp参加人数);
spState=(微调器)view.findViewById(R.id.sp_状态);
spIntroduceOnsiteContact=(微调器)视图
.findviewbyd(R.id.sp_自我介绍);
spWasHotelBooked=(微调器)视图。findViewById(R.id.sp_酒店预订);
seventPublic=getResources().getStringArray(R.array.public\u private);
eventPublic=new ArrayAdapter(mContext,
android.R.layout.simple\u spinner\u项目,seventPublic);
事件公众
.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
设置适配器(eventPublic);
syes=getResources().getStringArray(R.array.yes\u no);
eventyes=新阵列适配器(mContext,
android.R.layout.simple_微调器_项目,syes);
事件公众
.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
spEventContact.setAdapter(eventyes);
spadedEventItems.setAdapter(eventyes);
spPassMaterial.setAdapter(eventyes);
spIntroduceOnsiteContact.setAdapter(eventyes);
设置适配器(eventyes);
time=getResources().getStringArray(R.array.time\u frame);
eventTime=新阵列适配器(mContext,
android.R.layout.simple\u微调器(项目、时间);
活动时间
.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
设置适配器(eventTime);
people=getResources().getStringArray(R.array.people_);
eventPeople=newarrayadapter(mContext,
android.R.layout.simple_微调器_项目,人物);
事件人物
.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
sppeopleaAttached.setAdapter(eventPeople);
usa=getResources().getStringArray(R.array.usa);
usaStates=new ArrayAdapter(mContext,android.R.layout.simple\u spinner\u项目,美国);
美国
.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
setAdapter(美国各州);
tab1=(按钮)view.findViewById(R.id.tab1);
tab2=(按钮)view.findViewById(R.id.tab2);
表1.setOnClickListener(本);
表2.setOnClickListener(本);
layout1=(LinearLayout)view.findViewById(R.id.layout1);
layout2=(LinearLayout)view.findViewById(R.id.layout2);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.survey_addedit_碎片,容器,
假);
返回视图;
}
@凌驾
公共void onClick(视图arg0){
//TODO自动生成的方法存根
开关(arg0.getId()){
案例R.id.tab1: