从EditText获取值时出错

时间:2017-10-23 16:39:16

标签: java android

出于某种原因,我无法使用OnClickListener从EditText获取文本作为我的按钮。我想获取这些值并将它们放入data []然后将其传输到我的主要活动。由于某种原因,它打破了onClick()方法中的if语句。我检查了布局元素的所有ID,这不是问题。

以下是我片段的源代码:

package com.xivi0n.materialcalculator;



import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;


/**
 * A simple {@link Fragment} subclass.
*/
public class FirstFragment extends Fragment implements View.OnClickListener {

RadioGroup rd;
CheckBox cb;
Button calculate;
OnHeadlineSelectedListener sendData;
private EditText yEditText;
private EditText xEditText;
private RadioGroup type;

float data[] = new float[6];

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

public interface OnHeadlineSelectedListener {
    public void onArticleSelected(float data[]);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        sendData = (OnHeadlineSelectedListener)context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_first, container, false);

    rd = (RadioGroup) v.findViewById(R.id.isolationOptions);
    cb = (CheckBox)v.findViewById(R.id.isolationCB);
    xEditText = (EditText) v.findViewById(R.id.xValue);
    yEditText = (EditText) v.findViewById(R.id.yValue);
    type = (RadioGroup) v.findViewById(R.id.typeOf);
    calculate = (Button) v.findViewById(R.id.calculateButton);
    cb.setOnClickListener(this);
    calculate.setOnClickListener(this);
    return v;
}

public void onClick(View v) {
    if (v==cb) {
        if (cb.isChecked()) {
            rd.setVisibility(View.VISIBLE);
        } else {
            rd.setVisibility(View.INVISIBLE);
        }
    } else if (v==calculate){
        if ((xEditText.getText().toString()!="") && (yEditText.getText().toString()!="")){
            data[0] = (float)1;
            data[1] = Float.parseFloat(xEditText.getText().toString());
            data[2] = Float.parseFloat(yEditText.getText().toString());
            data[3] = (float) type.indexOfChild(getActivity().findViewById(type.getCheckedRadioButtonId()));
            if (cb.isChecked()){
                data[4] = (float)1;
                data[5] = (float) rd.indexOfChild(getActivity().findViewById(type.getCheckedRadioButtonId()));
            } else {
                data[4] = (float)0;
                data[5] = (float)0;
            }
            sendData.onArticleSelected(data);
        } else {
           Toast.makeText(getActivity(), "Invalid input, check all the parameters!", Toast.LENGTH_LONG).show();
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

使用.equals来比较字符串。

而不是((xEditText.getText().toString()!= ""))使用(!(xEditText.getText().toString().equals(""));作为if语句的条件。

答案 1 :(得分:0)

启用视图的ID:

public void onClick(View v) {
    switch (v.getId()){
    case R.id.isolationCB:
        //do something
        break;
    case R.id.calculateButton:
        //do something else
    }
}