当我点击(按钮x)并将int x值传递给另一个活动时,我想更改(int x)。 当我点击(按钮y)并将int y值传递给另一个活动时,改变(int y)。
如何在Android上做到这一点?
答案 0 :(得分:1)
I want to change (int x) when i click on (button x)
这是微不足道的,你为你的按钮创建了一个监听器,你在里面改变你的变量x(在你的活动A中定义)
and pass int x value to another activity.
您需要将此x
值放在Intent bundle
and change (int y) when i click on (button y) and pass int y value to another activity.
这与上面完全相同,但对于变量y /按钮y
是家庭作业吗?
答案 1 :(得分:1)
使用SharedPreference。保存在A1中并在A2中检索,反之亦然。
<强>初始化强>
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
存储数据
editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long
editor.commit(); // commit changes
检索数据
// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean
删除数据
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
清算存储空间
editor.clear();
editor.commit(); // commit changes
答案 2 :(得分:1)
1活动
int x=value;
Intent i=new Intent(this, secActiv.class);
i.putintextra("x",x);
startactivity(i)
2活动
oncreate(){...
Intent i=getintent();
int x=i.getintextra("x",null);