in片段从edittext获取值并进行计算

pnwntuvh  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(316)

我刚刚把我的第一个片段添加到我的应用程序中。现在我想做一些计算。它有4个编辑文本字段和一个计算按钮。
如果我为按钮干杯,就会显示干杯!
但没有结果。
我做错什么了?

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class MessageFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_message, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.findViewById(R.id.bt_calculate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(getActivity(), "Hi, connection is working!!!", Toast.LENGTH_SHORT).show();

                EditText input1 = (EditText) view.findViewById(R.id.et_input1);
                EditText input2 = (EditText) view.findViewById(R.id.et_input2);
                EditText input3 = (EditText) view.findViewById(R.id.et_input3);
                EditText input4 = (EditText) view.findViewById(R.id.et_inputSp);
                try {
                    int a = Integer.parseInt(input1.getText().toString());
                    int b = Integer.parseInt(input2.getText().toString());
                    int c = Integer.parseInt(input3.getText().toString());
                    int d = Integer.parseInt(input4.getText().toString());
                    int calc = (a * 10) + (b * 20) + (c * 30) + (d * 15);
                    TextView answer = (TextView) view.findViewById(R.id.tv_result);
                    answer.setText("" + calc);
                } catch (Exception e) {
                    //Textfield empty
                }

            }

        });
    }
}

这是我的xml文件。课文是德语的,希望你能听懂。否则请问我。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="39dp"
        android:layout_centerHorizontal="true"
        android:text="Handelsschiff Event Rechner"
        android:textColor="#F44336"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/et_input1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:ems="10"
        android:hint="3. Klasse Marken"
        android:inputType="number" />

    <EditText
        android:id="@+id/et_input2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_input1"
        android:ems="10"
        android:hint="2. Klasse Marken"
        android:inputType="number" />

    <EditText
        android:id="@+id/et_input3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_input2"
        android:ems="10"
        android:hint="1. Klasse Marken"
        android:inputType="number" />

    <EditText
        android:id="@+id/et_inputSp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_input3"
        android:layout_alignParentStart="true"
        android:hint="SP Klasse Marken"
        android:inputType="number" />

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="311dp"
        android:hint="Hinweis" />

    <Button
        android:id="@+id/bt_calculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_inputSp"
        android:layout_alignParentStart="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="154dp"
        android:layout_marginTop="154dp"
        android:text="Calculate" />

</RelativeLayout>
shyt4zoc

shyt4zoc1#

像这样吗-
在开始时初始化视图,要在onclick中访问它们,请将它们设置为final。
我们不需要像(edittext)findviewbyid(r.id.edittext1)这样强制转换视图。

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

                final EditText input1 = view.findViewById(R.id.et_input1);
                final EditText input2 = view.findViewById(R.id.et_input2);
                final EditText input3 = view.findViewById(R.id.et_input3);
                final EditText input4 = view.findViewById(R.id.et_inputSp);
                final TextView answer = view.findViewById(R.id.tv_result);

        view.findViewById(R.id.bt_calculate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                            Toast.makeText(getActivity(), "Hi, connection is working!!!", Toast.LENGTH_SHORT).show();

            try {
                int a = Integer.parseInt(input1.getText().toString());
                int b = Integer.parseInt(input2.getText().toString());
                int c = Integer.parseInt(input3.getText().toString());
                int d = Integer.parseInt(input4.getText().toString());
                int calc = (a * 10) + (b * 20) + (c * 30) + (d * 15);
                answer.setText("" + calc);
            } catch (Exception e) {
                //Textfield empty
            }
            }
        });

输入:a=2,b=2,c=2,d=2输出:150

相关问题