英文:
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(), "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>
答案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(), "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
}
}
});
For input: a=2,b=2,c=2,d=2 Output: 150
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论