简体   繁体   English

从EditText获取值时出错

[英]Error getting value from EditText

For some reason I can't get text from EditText with OnClickListener for my button. 出于某种原因,我无法从带有OnClickListener的按钮的EditText中获取文本。 I want to get these values and place them into data[] and then transfer it to my main activity. 我想获取这些值并将其放入data [],然后将其传输到我的主要活动中。 For some reason it breaks on if statement in onClick() method. 由于某种原因,它会中断onClick()方法中的if语句。 I've checked all the ids of layout elements and that's not the problem. 我已经检查了布局元素的所有id,这不是问题。

Here is the source code for my fragment: 这是我的片段的源代码:

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();
        }
    }
}

}

Use .equals to compare strings. 使用.equals比较字符串。

Instead of ((xEditText.getText().toString()!= "")) use (!(xEditText.getText().toString().equals("")); as the condition of if statement. 代替((xEditText.getText().toString()!= ""))使用(!(xEditText.getText().toString().equals(""));作为if语句的条件。

Switch on the id of the view: 打开视图的ID:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM