Android getIntExtra() 和 putExtra()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12318791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getIntExtra() and putExtra()?
提问by Pariya
I read Hello Android book and i dont understan the following code. I dont know, what to do getIntExtra() and putExtra() int this code.
我读了你好 Android 书,但我不理解以下代码。我不知道该怎么做 getIntExtra() 和 putExtra() int 这段代码。
private void startGame(int i) {
Log.d(TAG, "clicked on " + i);
Intent intent = new Intent(Sudoku.this, Game.class);
intent.putExtra(Game.KEY_DIFFICULTY, i);
startActivity(intent);
}
Game.java
游戏.java
public class Game extends Activity {
private static final String TAG = "Sudoku" ;
public static final String KEY_DIFFICULTY ="org.example.sudoku.difficulty" ;
public static final int DIFFICULTY_EASY = 0;
public static final int DIFFICULTY_MEDIUM = 1;
public static final int DIFFICULTY_HARD = 2;
private int puzzle[] = new int[9 * 9];
private PuzzleView puzzleView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate" );
int diff = getIntent().getIntExtra(KEY_DIFFICULTY,DIFFICULTY_EASY);
puzzle = getPuzzle(diff);
calculateUsedTiles();
puzzleView = new PuzzleView(this);
setContentView(puzzleView);
puzzleView.requestFocus();
}
// ...
}
The problem I have is that you are setting a local integer (‘diff') within the Game class. with a default value of zero (easy) and then immediately passing it into the getPuzzle method …. how does the user input value ( the real value all being well) ever find it's way into the getPuzzle method?
我的问题是您在 Game 类中设置了一个本地整数('diff')。默认值为零(简单),然后立即将其传递到 getPuzzle 方法中...... 用户输入值(真实值都很好)是如何进入 getPuzzle 方法的?
回答by Heinzi
This code:
这段代码:
Intent intent = new Intent(Sudoku.this, Game.class);
intent.putExtra(Game.KEY_DIFFICULTY, i);
startActivity(intent);
creates an intent which, when executed with startActivity
, does two things:
创建一个意图,当用 执行时startActivity
,它会做两件事:
- It starts a new activity of class
Game
(specified by the parameterGame.class
) and - it passes
i
(= the user input) into the activity, tagged with the string content ofKEY_DIFFICULTY
.
- 它开始一个新的类活动
Game
(由参数指定Game.class
)和 - 它将
i
(= 用户输入)传递到活动中,并用KEY_DIFFICULTY
.
In the activity, this line:
在活动中,这一行:
int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
reads the value that was set for KEY_DIFFICULTY
in the intent used to start the activity. Hence, diff
now contains the user-selected value (or DIFFICULTY_EASY
, if the activity is started through a different intent which did notset KEY_DIFFICULTY
).
读取在KEY_DIFFICULTY
用于启动活动的意图中设置的值。因此,diff
现在包含用户选择的值(或者DIFFICULTY_EASY
,如果活动是通过未设置的不同意图启动的KEY_DIFFICULTY
)。
回答by gmazlami
Intents are used to start an activity programmatically in android. The intent can carry data, which you pass to the new started activity.
Intent 用于在 android 中以编程方式启动 Activity。Intent 可以携带数据,您将这些数据传递给新启动的 Activity。
startGame(int i)
starts the new game activity with an intent. Putting an extra to an intent means, you are passing data over to the intent. The started activity (in your case the Game.java) then can acceess this extra from the intent.
以意图开始新的游戏活动。为意图添加额外内容意味着,您将数据传递给意图。开始的活动(在你的例子中是 Game.java)然后可以从意图中访问这个额外的东西。
It is a mechanism to pass data between activities.
它是一种在活动之间传递数据的机制。
The first argument (KEY_DIFFICULTY) is the key by which the extra is identified. So if you put an extra to an intent with key 'mykeyexample' you will have to do a get with the same key 'mykeyexample' in another activity to get the desired extra from the intent.
第一个参数 (KEY_DIFFICULTY) 是标识额外内容的键。因此,如果您使用键 'mykeyexample' 为意图添加额外内容,则必须在另一个活动中使用相同的键 'mykeyexample' 进行 get 操作,以便从该意图中获取所需的额外内容。
Hope this helps
希望这可以帮助