从编辑文本中获取值并进行一些计算

时间:2016-07-13 07:08:32

标签: java android android-fragments android-edittext textview

我有一个包含3个编辑文本和1个文本视图的片段。我想从这些编辑文本中获取输入。但它强行关闭。问题是什么? 我的代码:

View view = inflater.inflate(R.layout.bmi_fragment,
            container, false);
    EditText age = (EditText) view.findViewById(R.id.editText_age);
    EditText weight = (EditText) view.findViewById(R.id.editText_weight);
    EditText height = (EditText) view.findViewById(R.id.editText_height);
    int age_value = Integer.parseInt(age.getText().toString());
    int weight_value = Integer.parseInt(weight.getText().toString());
    int height_value = Integer.parseInt(height.getText().toString());
    int calc = (weight_value / ((height_value / 100) * (height_value / 100)));
    TextView dd = (TextView) view.findViewById(R.id.result_bmi_textView);
    dd.setText(calc);

4 个答案:

答案 0 :(得分:1)

像这样使用setText()

View view = inflater.inflate(R.layout.bmi_fragment,
            container, false);
    EditText age = (EditText) view.findViewById(R.id.editText_age);
    EditText weight = (EditText) view.findViewById(R.id.editText_weight);
    EditText height = (EditText) view.findViewById(R.id.editText_height);
    int age_value = Integer.parseInt(age.getText().toString());
    int weight_value = Integer.parseInt(weight.getText().toString());
    int height_value = Integer.parseInt(height.getText().toString());
    int calc = (weight_value / ((height_value / 100) * (height_value / 100)));
    TextView dd = (TextView) view.findViewById(R.id.result_bmi_textView);
    dd.setText(""+calc);

答案 1 :(得分:1)

您正在使用use_frameworks! platform :ios, '7.0' pod 'Firebase/Messaging' target 'FCM' do end target 'FCMSwift' do end target 'FCMTests' do end 。但是你传递.SetText()类型。这是一个问题。所以你需要转换int

尝试,

int to String

或者您可以使用 dd.setText("Value : " + calc); // concat with String dd.setText("" + calc); // concat with empty string

Integer.toString(int)

或使用 dd.setText(Integer.toString(calc));

String.valueOf(int)

这可能有助于你

没有按钮:需要添加dd.setText(String.valueOf(calc));

TextChangedListener

答案 2 :(得分:1)

像这样使用

View view = inflater.inflate(R.layout.bmi_fragment,
        container, false);
EditText age = (EditText) view.findViewById(R.id.editText_age);
EditText weight = (EditText) view.findViewById(R.id.editText_weight);
EditText height = (EditText) view.findViewById(R.id.editText_height);
int age_value = Integer.parseInt(age.getText().toString());
int weight_value = Integer.parseInt(weight.getText().toString());
int height_value = Integer.parseInt(height.getText().toString());
int calc = (weight_value / ((height_value / 100) * (height_value / 100)));
TextView dd = (TextView) view.findViewById(R.id.result_bmi_textView);
dd.setText(String.valueOf(calc));

答案 3 :(得分:1)

为空EditText添加控件

View view = inflater.inflate(R.layout.bmi_fragment,
        container, false);
EditText age = (EditText) view.findViewById(R.id.editText_age);
EditText weight = (EditText) view.findViewById(R.id.editText_weight);
EditText height = (EditText) view.findViewById(R.id.editText_height);
try {
        int age_value = Integer.parseInt(age.getText().toString());
        int weight_value = Integer.parseInt(weight.getText().toString());
        int height_value = Integer.parseInt(height.getText().toString());
        int calc = (weight_value / ((height_value / 100) * (height_value / 100)));
        TextView dd = (TextView) findViewById(R.id.textView);
        dd.setText(calc+"");
} catch (Exception e){
    //Textfield empty
}