如何在Android中检查用户是否从微调器中选择了项目?

时间:2016-07-04 09:43:00

标签: android android-spinner

我的活动中有一个Spinner,其中包含微调器0位置的一些项目我添加“选择操作员”并从位置1项开始。并在向服务器发送请求之前创建一个条件,检查是否有用户如果从1开始选择项目,则从位置1选择项目然后提交按钮变为启用其他方式保持禁用。 我怎么能这样做

代码: -

private final BroadcastReceiver m_oInternetChecker = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        checkforEmpty();
    }
};
public void checkforEmpty() {
    m_szMobileNumber = m_InputMobile.getText().toString().trim();
    m_szAmount = m_InputAmount.getText().toString().trim();

    if (NetworkUtil.isConnected(getActivity())) {
        if (m_szMobileNumber.length() == 13) {
            if (m_szAmount.length() != 0) {
                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) {
                        if (m_OperatorSpinner.getSelectedItemPosition() < 1) {
                            TextView errorText = (TextView)m_OperatorSpinner.getSelectedView();
                            errorText.setError("");
                            errorText.setTextColor(Color.RED);//just to highlight that this is an error
                            errorText.setText("Please Select Operator");//changes the selected item text to this
                            CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Please select operator", getActivity());
                        } else {
                            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 {
        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));
    }
}
    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) {

        }
    });

2 个答案:

答案 0 :(得分:0)

onItemSelected的{​​{1}}中,您将收到您在OnItemSelectedListener中选择的位置。

Spinner

答案 1 :(得分:0)

试试这个

if