我只是想出了如何使用意图将变量传递给其他活动,但是我发现它有一个限制,它只能将一个变量传递给一个活动。 我想知道如何在多个活动中共享变量,就像我在活动1中拥有此变量,并在活动2和活动3中使用它一样。
这是我的活动1,我想在其他活动中使用的变量是这个 (double ans =(90 + 4.8 * nh + 13.4 * nw-5.7 * na);) 我想出了如何将其传递给活动2的方法,但我无法将其传递给活动3的方法。是否有一种简单的方法可以将变量传递给多个活动,因为我对Android和OOP还是很陌生的,所以我有很多东西并非仅从搜索中获得收益。非常感谢!
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_male);
age =(TextView)findViewById(R.id.age);
height=(TextView)findViewById(R.id.heigth);
weight =(TextView)findViewById(R.id.weigth);
result =(TextView)findViewById(R.id.result);
eage=(EditText)findViewById(R.id.eage);
eheight=(EditText)findViewById(R.id.eheight);
eweight=(EditText)findViewById(R.id.eweight);
calculate=(Button)findViewById(R.id.calculate);
back =(Button)findViewById(R.id.back);
next =(Button)findViewById(R.id.next);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double na=Double.parseDouble(eage.getText().toString());
double nh=Double.parseDouble(eheight.getText().toString());
double nw=Double.parseDouble(eweight.getText().toString());
double ans=(90+4.8*nh+13.4*nw-5.7*na);
result.setText("Your BMR is "+ans);
}
});
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(MALE.this,MainActivity.class);
startActivity(i);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MALE.this, MALERUN.class);
double na=Double.parseDouble(eage.getText().toString());
double nh=Double.parseDouble(eheight.getText().toString());
double nw=Double.parseDouble(eweight.getText().toString());
double ans=(90+4.8*nh+13.4*nw-5.7*na);
result.setText("Your BMR is "+ans);
i.putExtra("answer",ans);
i.putExtra("age",na);
i.putExtra("height",nh);
i.putExtra("weight",nw);
startActivity(i);
}
});
}
答案 0 :(得分:1)
使用SingleTon
模式。
public class SingletonClass {
private double yourValue;
private static SingletonClass sSoleInstance;
private SingletonClass() {
} // private constructor.
public static SingletonClass getInstance() {
if (sSoleInstance == null) {
sSoleInstance = new SingletonClass();
}
return sSoleInstance;
}
public void setValue(double val) {
yourValue = val;
}
public double getValue() {
return yourValue;
}
}
从Activity1
设置变量
SingletonClass.getInstance().setValue(yourValue);
现在您可以在整个应用程序中访问此值。
SingletonClass.getInstance().getValue();
有关更多详细信息,click here
答案 1 :(得分:0)
使用 SharedPreferences 即可获取。这里是示例
next.setOnClickListener{
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("answer",ans);
editor.putString("age",""+na);
editor.putString("height",""+nh);
editor.putString("weight",""+nw);
editor.commit();
Intent i=new Intent(MALE.this,MainActivity.class);
startActivity(i);
}
任何其他活动,您都可以在下面获得它
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(this);
String answer = sharedPref.getString("answer",null);
String age = sharedPref.getString("age",null);
Log.e("resultsss",""+answer + " age : "+age);
}
解决方案2:
要么必须使用Sqlite数据库
建议:如果您希望使用的字段很少,
SharedPreferences
如果拥抱数据少,最好使用Sqlite 数据库
答案 2 :(得分:-1)
您可以声明一个变量:
public class MainActivity extends AppCompatActivity {
// Like below
public static double ans;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
}
}
像这样从任何地方调用它:
MainActivity.ans;
您可以随时声明一个不同的变量,它将在每个Activity中更改。
只需这样做:
MainActivity.ans = 123.123;