按下“发送数据”按钮后,我想从MainActivity向SecondActivity发送两个数字。有了这个,我没问题,一切正常。 但是,然后,我希望在SecondActivity中对接收到的数字进行加,减,乘和除运算。如果我返回MainActivity,则返回结果。但是我做不到,该怎么办?
这是我的MainActivity:
public class MainActivity extends AppCompatActivity {
EditText FirstNumber, SecondNumber;
TextView Results, Addition, Subtraction, Multiplication, Division;
Button SendData;
float Add, Sub, Multi, Div;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirstNumber = findViewById(R.id.editText);
SecondNumber = findViewById(R.id.editText2);
Results = findViewById(R.id.textView);
Addition = findViewById(R.id.textView2);
Subtraction = findViewById(R.id.textView3);
Multiplication = findViewById(R.id.textView4);
Division = findViewById(R.id.textView5);
SendData = findViewById(R.id.button);
SendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float Number1 = Float.parseFloat(FirstNumber.getText().toString());
float Number2 = Float.parseFloat(SecondNumber.getText().toString());
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("Number1", Number1);
i.putExtra("Number2", Number2);
startActivity(i);
}
});
}
}
这是我的SecondActivity:
public class SecondActivity extends AppCompatActivity {
Float Number1;
Float Number2;
Float Add;
Float Sub;
Float Multi;
Float Div;
TextView FirstNumb;
TextView SecondNumb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
FirstNumb = findViewById(R.id.textView7);
SecondNumb = findViewById(R.id.textView8);
Number1 = getIntent().getFloatExtra("Number1", 0);
Number2 = getIntent().getFloatExtra("Number2", 0);
FirstNumb.setText("First Number: " + String.valueOf(Number1));
SecondNumb.setText("Second Number: " + String.valueOf(Number2));
}
}
``;
答案 0 :(得分:0)
在第二个活动中,执行所有算术运算后,使用sharedPreferences
保存数据
然后在mainActivity的oncreate中检索数据。
您可以使用保存数据
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("name", "Elena"); editor.putInt("idName", 12); editor.commit();
使用
检索数据SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); String restoredText = prefs.getString("text", null); if (restoredText != null) { String name = prefs.getString("name", "No name defined");//"No name defined" is the default value. int idName = prefs.getInt("idName", 0); //0 is the default value. }
答案 1 :(得分:0)
作为此通知的结果,您可以将数据从SecondActivity取回MainActivity,这是MainActivity的sendData单击中的此行startActivityForResult(i, REQUEST_CODE);
SendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float Number1 = Float.parseFloat(FirstNumber.getText().toString());
float Number2 = Float.parseFloat(SecondNumber.getText().toString());
Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("Number1", Number1);
i.putExtra("Number2", Number2);
startActivityForResult(i, 2);
}
});
并在与此相同的MainActivity中重写onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("result");
textView1.setText(message);
}
}
在SecondActivity中,您可以像这样发送回数据
sendResultback.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String operation = String.valueOf(Number1+Number2);
Intent intent=new Intent();
intent.putExtra("MESSAGE",operation);
setResult(2,intent);
finish();//finishing activity
}
});
答案 2 :(得分:-2)
您做得很好,并且您希望在执行所有拟气操作后将数据发送到第一个活动。 您可以使用这种方式:-
使用共享首选项
1 )执行所有操作后,将数据保存在“共享首选项”中,并在您返回第一个活动时获取数据。
您可以使用SharedPreference轻松添加/检索数据。遵循以下步骤:-
定义SharedPreference.Editor
SharedPreferences.Editor编辑器;
将值放入SharedPreference中,就像:-
编辑器= getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit();
editor.putInt(“ sum”,value_of_sum);
editor.putInt(“ difference”,value_of_difference);
editor.putInt(“ multiply”,value_of_mul);
editor.putInt(“ div”,value_of_div);
editor.apply();
在“第二个活动”上检索数据,例如:-
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE);
int sum = prefs.getInt(“ sum”,null);
int dif = prefs.getInt(“ difference”,null);
int mul = prefs.getInt(“ multiply”,null);
int div = prefs.getInt(“ div”,null);
您可以从此link获取帮助,以进一步了解SharedPreferences。希望对您有所帮助。谢谢.....