问题
I want to change (int x) when i click on (button x) and pass int x value to another activity. and change (int y) when i click on (button y) and pass int y value to another activity.
how to do that on android ?
回答1:
I want to change (int x) when i click on (button x)
this is trivial, you create listener for your button and inside it you alter your variable x (defined in your activity A)
and pass int x value to another activity.
you need to put this x
value inside Intent bundle
and change (int y) when i click on (button y) and pass int y value to another activity.
this is exactly the same as above, but for variable y/button y
is it a homework?
回答2:
Use SharedPreference. Save in A1 and retrieve in A2 and vice-versa.
Initialization
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
Storing Data
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
Retrieving Data
// 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
Deleting Data
editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes
Clearing Storage
editor.clear();
editor.commit(); // commit changes
回答3:
1 activity
int x=value;
Intent i=new Intent(this, secActiv.class);
i.putintextra("x",x);
startactivity(i)
2 activity
oncreate(){...
Intent i=getintent();
int x=i.getintextra("x",null);
来源:https://stackoverflow.com/questions/21144158/how-to-change-the-int-value-onclick-and-pass-it-to-another-activity-on-android