我知道,这个问题已经存在于这个站点和其他站点中,但我仍然尝试找到解决方案,因为到目前为止每个解决方案都失败了。
我有一个活动,它有 2 个片段。在第一个片段中,我有 5 个 EditText,这个输入我需要传递给第二个片段,但是......我在这个包中还有一个 Java 类,它必须使用这个输入的值,而这个结果我必须显示在第二个片段中。这是一个平均计算器。
这是我的第一个片段:
package com.example.afotel;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class gyorsSzam extends Fragment {
EditText gradeFive, gradeFour, gradeThree, gradeTwo, gradeOne;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gyors_szam, container, false);
Button calculate = (Button) view.findViewById(R.id.gGyors);
gradeFive = (EditText) view.findViewById(R.id.numberOfGardeFive);
gradeFour = (EditText) view.findViewById(R.id.numberOfGardeFour);
gradeThree = (EditText) view.findViewById(R.id.numberOfGardeThree);
gradeTwo = (EditText) view.findViewById(R.id.numberOfGardeTwo);
gradeOne = (EditText) view.findViewById(R.id.numberOfGardeOne);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.gyorsSzamlalas, new jegyekFelLe());
fr.commit();
String gradeFiveSt = gradeFive.getText().toString();
String gradeFourSt = gradeFour.getText().toString();
String gradeThreeSt = gradeThree.getText().toString();
String gradeTwoSt = gradeTwo.getText().toString();
String gradeOneeSt = gradeOne.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Five", gradeFiveSt );
bundle.putString("Four", gradeFourSt );
bundle.putString("Three", gradeThreeSt );
bundle.putString("Two", gradeTwoSt );
bundle.putString("Ones", gradeOneeSt );
}
});
return view;
}
}
这是我的第二个片段:
package com.example.afotel;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class jegyekFelLe extends Fragment {
public jegyekFelLe() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jegyek_fel_le, container, false);
Bundle bundle = getArguments();
String gradeFive = bundle.getString("Five");
String gradeFour = bundle.getString("Four");
String gradeThree = bundle.getString("Three");
String gradeTwo = bundle.getString("Two");
String gradeOne = bundle.getString("One");
TextView upFive = (TextView) rootView.findViewById(R.id.upFive);
upFive.setText(gradeFive);
TextView felNegyes = (TextView) rootView.findViewById(R.id.);
upFour.setText(gradeFour);
TextView upThree= (TextView) rootView.findViewById(R.id.upThree);
upThree.setText(gradeThree);
TextView upTwo = (TextView) rootView.findViewById(R.id.upTwo);
upTwo.setText(gradeTwo);
TextView downOne = (TextView) rootView.findViewById(R.id.dowbOne);
leEgyes.setText(gradeOne);
return rootView;
}
}
如果代码包含 TextView upFive...(etc.) 部分,如果我点击计算按钮,程序就会崩溃。我不知道有什么问题,因为我做了这个视频所说的一切:https : //www.youtube.com/watch?v=_4i5Jk5RnFA
我知道,这个例子中的 Java Class 没有被使用,那是因为我的代码有这个问题。首先,我想知道如何在两个 Fragment 之间传递数据,然后在我编写之后从这 5 个 EditText 中计算出我需要的结果。我是 Android Studio 的初学者,所以我需要简单、难以理解的解决方案。
我希望你能理解我的问题,对不起我的英语。
在提交事务之前设置包并向片段添加参数。您可能需要将代码更改为此结构:
Bundle bundle= new Bundle();
//add items to the bundle then begin transacting
Fragment jegyekFelLe= new jegyekFelLe();
jegyekFelLe.setArguments(bundle);
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.gyorsSzamlalas,jegyekFelLe );
fr.commit();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句