如何在android中检查用户是否从spinner中选择了项目?

时间:2016-07-01 02:52:57

标签: android android-spinner

我有一个包含微调器的活动,它有一些项目列表,我有一个名为“submit”的按钮,第一次提交按钮变为禁用它只有在用户从微调器中选择项目后才能启用如果没有选择,则提交仍然禁用,为此我必须在按钮点击时生成条件。我怎么能这样做 代码: -

m_OperatorSpinner = (Spinner) m_Main.findViewById(R.id.operator_spinner);
    ArrayAdapter<COperatorStorage> adapter = new ArrayAdapter<COperatorStorage>(getActivity(), R.layout.recharge_item, CRechargeOperatorList.item);
    m_OperatorSpinner.setAdapter(adapter);
    m_OperatorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            COperatorStorage operatorName = CRechargeOperatorList.item[position];
            selectedOperatorCode = operatorName.code;
            Log.e(TAG, "Operator code:-" + selectedOperatorCode);

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
public void checkforEmpty() {
    if (NetworkUtil.isConnected(getActivity())) {
        if (m_szMobileNumber.length() == 13) {
            if (m_szAmount.length() != 0) {
                if (selectedOperatorCode!=null){
                    m_SubmitButton.setEnabled(true);
                    m_SubmitButton.setBackgroundColor(Color.rgb(0, 80, 147));// set background color on eabled
                    m_SubmitButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            proceedToRecharge(v);
                        }
                    });
                }else {
                    m_SubmitButton.setEnabled(false);
                    m_SubmitButton.setBackgroundColor(Color.rgb(192, 192, 192));
                }

            } else {
                m_SubmitButton.setEnabled(false);
                m_SubmitButton.setBackgroundColor(Color.rgb(192, 192, 192));
            }
        } else {
            m_SubmitButton.setEnabled(false);
            m_SubmitButton.setBackgroundColor(Color.rgb(192, 192, 192));
        }
    } else {
        try {
            CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No Internet Connection Available", getActivity());

        } catch (Exception e) {
            e.printStackTrace();
        }
        m_SubmitButton.setEnabled(false);
        m_SubmitButton.setBackgroundColor(Color.rgb(192, 192, 192));
    }
}

2 个答案:

答案 0 :(得分:0)

寻找位置,你只需测试位置:

示例:

if(position==1){
   //what to do here
}else if(position==2){
   //what to do here
}

答案 1 :(得分:-1)

在onItemSelected中设置一些布尔标志为true,并根据标志状态启用提交按钮。

boolean isItemSelected ;

    m_OperatorSpinner = (Spinner) m_Main.findViewById(R.id.operator_spinner);
        ArrayAdapter<COperatorStorage> adapter = new ArrayAdapter<COperatorStorage>(getActivity(), R.layout.recharge_item, CRechargeOperatorList.item);
        m_OperatorSpinner.setAdapter(adapter);
        m_OperatorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                COperatorStorage operatorName = CRechargeOperatorList.item[position];
                selectedOperatorCode = operatorName.code;
                Log.e(TAG, "Operator code:-" + selectedOperatorCode);
    if(position!=0){
    isItemSelected = true;
}else{
isItemSelected =false;
}

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        public void checkforEmpty() {
            if (NetworkUtil.isConnected(getActivity())) {
                if ((m_szMobileNumber.length() == 13) &&
                     (m_szAmount.length() != 0) &&(selectedOperatorCode!=null) && isItemSelected ){
                            m_SubmitButton.setEnabled(true);
                            m_SubmitButton.setBackgroundColor(Color.rgb(0, 80, 147));// set background color on eabled
                            m_SubmitButton.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    proceedToRecharge(v);
                                }
                            });
                        }else {
                            m_SubmitButton.setEnabled(false);
                            m_SubmitButton.setBackgroundColor(Color.rgb(192, 192, 192));
                        }

                    } else {
                        m_SubmitButton.setEnabled(false);
            } else {
                try {
                    CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No Internet Connection Available", getActivity());

                } catch (Exception e) {
                    e.printStackTrace();
                }
                m_SubmitButton.setEnabled(false);
                m_SubmitButton.setBackgroundColor(Color.rgb(192, 192, 192));
            }
        }