从编辑框中获取数值并进行计算。

huangapple go评论79阅读模式
英文:

In fragment get value from editText and do calculation

问题

I just added my first fragment into my app. Now I want to do some calculation there. It has 4 editText fields and a Calculate button.

If I do a toast for the button, the toast is shown!

But there is no result shown.

What am I doing wrong?

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
                }

            }

        });
    }
}

And here is my XML File. The texts are in German, I hope you understand everything. Otherwise please ask me.

<?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>
英文:

I just added my first fragment into my app. Now I want to do some calculation there. It has 4 editText fields and a Calculate button.

If I do a toast for the button, the toast is shown!

But there is no result shown.

What am I doing wrong?

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(), &quot;Hi, connection is working!!!&quot;, 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(&quot;&quot; + calc);
} catch (Exception e) {
//Textfield empty
}
}
});
}
}

And here is my XML File. The texts are in German, I hope you understand everything. Otherwise please ask me.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@android:color/white&quot;&gt;
&lt;TextView
android:id=&quot;@+id/tv_title&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;39dp&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:text=&quot;Handelsschiff Event Rechner&quot;
android:textColor=&quot;#F44336&quot;
android:textSize=&quot;24sp&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/et_input1&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@id/tv_title&quot;
android:ems=&quot;10&quot;
android:hint=&quot;3. Klasse Marken&quot;
android:inputType=&quot;number&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/et_input2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/et_input1&quot;
android:ems=&quot;10&quot;
android:hint=&quot;2. Klasse Marken&quot;
android:inputType=&quot;number&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/et_input3&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/et_input2&quot;
android:ems=&quot;10&quot;
android:hint=&quot;1. Klasse Marken&quot;
android:inputType=&quot;number&quot; /&gt;
&lt;EditText
android:id=&quot;@+id/et_inputSp&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/et_input3&quot;
android:layout_alignParentStart=&quot;true&quot;
android:hint=&quot;SP Klasse Marken&quot;
android:inputType=&quot;number&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/tv_result&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_alignParentTop=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_marginTop=&quot;311dp&quot;
android:hint=&quot;Hinweis&quot; /&gt;
&lt;Button
android:id=&quot;@+id/bt_calculate&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_below=&quot;@+id/et_inputSp&quot;
android:layout_alignParentStart=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_gravity=&quot;center_horizontal&quot;
android:layout_marginStart=&quot;154dp&quot;
android:layout_marginTop=&quot;154dp&quot;
android:text=&quot;Calculate&quot; /&gt;
&lt;/RelativeLayout&gt;

答案1

得分: 1

像这样操作 -

在开始时初始化您的视图,并在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(), "嗨,连接正常!", 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) {
//文本字段为空
}
}
});

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

英文:

Do it like this -

Initialize your views in the beginning and to access them inside onclick, make them final.

And we don't need to cast views like (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(), &quot;Hi, connection is working!!!&quot;, 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(&quot;&quot; + calc);
} catch (Exception e) {
//Textfield empty
}
}
});

For input: a=2,b=2,c=2,d=2 Output: 150

huangapple
  • 本文由 发表于 2020年9月6日 05:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63758614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定