我阅读了有关 getInt() 方法的文档:
public int getInt(字符串键)
返回与给定键关联的值,如果给定键不存在所需类型的映射,则返回 0。
参数:
键入一个字符串
返回:
一个 int 值
但我不知道它到底返回了什么。
那个IDkey
在R.java中还是没有别的???
我阅读了有关 getInt() 方法的文档:
public int getInt(字符串键)
返回与给定键关联的值,如果给定键不存在所需类型的映射,则返回 0。
参数:
键入一个字符串
返回:
一个 int 值
但我不知道它到底返回了什么。
那个IDkey
在R.java中还是没有别的???
它使用相同的密钥返回您放入该捆绑包中的任何内容。
Bundle bundle = new Bundle();
bundle.putInt("KEY", 1);
int value = bundle.getInt("KEY"); // returns 1
它只是一个映射/字典数据类型,您可以在其中将字符串值与其他内容映射。如果您有其他数据类型,则应为该数据类型使用适当的 put/get 方法。
没有比举个例子更好的了
假设您有两个活动:Activity1 和 Activity2,并且您想传递数据 beetwen 然后:
活动1
private static final String MY_KEY = "My Key"
Intent intent = new Intent(Activity1.this, Activity2.class);
Bundle b = new Bundle();
b.putInt(MY_KEY, 112233);
intent.putExtras(b);
startActivity(intent);
活动二
private static final String MY_KEY = "My Key"
Bundle b = getIntent().getExtras();
int value = b.getInt(MY_KEY , 0);
//value now have the value 112233
什么意思是“返回与给定键关联的值,或者如果给定键不存在所需类型的映射,则返回 0。 ”在这个例子中?
使用 Bundle,您使用键“MY_KEY”将值112233从活动 1 发送到活动 2。因此,“MY_KEY”与 112233 相关联。
如您所见,有第二个参数“0”。
它是默认值。在 Bundle 不包含数据的情况下,您将收到“0”(默认值)。