ben*_*eel 35 android integer bundle
我想将一个整数的新值从一个Activity传递给另一个Activity.即:
活动B包含一个
integer[] pics = { R.drawable.1, R.drawable.2, R.drawable.3}
Run Code Online (Sandbox Code Playgroud)
我希望活动A将新值传递给活动B:
integer[] pics = { R.drawable.a, R.drawable.b, R.drawable.c}
Run Code Online (Sandbox Code Playgroud)
以某种方式通过
private void startSwitcher() {
Intent myIntent = new Intent(A.this, B.class);
startActivity(myIntent);
}
Run Code Online (Sandbox Code Playgroud)
我可以设置这个整数值.
我知道这可以通过捆绑包以某种方式完成,但我不确定如何将这些值从活动A传递到活动B.
Par*_*ani 113
这很简单.在发件人方面,使用Intent.putExtra:
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
Run Code Online (Sandbox Code Playgroud)
在接收方,使用Intent.getIntExtra:
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Run Code Online (Sandbox Code Playgroud)
它们是两种可用于传递整数的方法.一个如下所示.
一类
Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
Run Code Online (Sandbox Code Playgroud)
B.class
Intent intent = getIntent();
int intValue = intent.getIntExtra("intVariableName", 0);
Run Code Online (Sandbox Code Playgroud)
另一种方法将整数转换为字符串并使用以下代码.
一类
Intent intent = new Intent(A.this, B.class);
Bundle extras = new Bundle();
extras.putString("StringVariableName", intValue + "");
intent.putExtras(extras);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
上面的代码将您的整数值作为字符串传递给B类.在B类中,获取字符串值并再次转换为整数,如下所示.
B.class
Bundle extras = getIntent().getExtras();
String stringVariableName = extras.getString("StringVariableName");
int intVariableName = Integer.parseInt(stringVariableName);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
120045 次 |
| 最近记录: |