理解getIntExtra()参数

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details);$$ // pull the turtle's ID out of the intent that the MainActivity used to load me Intent intent = getIntent(); int id = intent.getIntExtra("turtle_id", R.id.leo); String text = ""; if (id == R.id.leo) { text = TURTLE_DETAILS[0]; } else if (id == R.id.mike) { text = TURTLE_DETAILS[1]; } else if (id == R.id.don) { text = TURTLE_DETAILS[2]; } else { // if (id == R.id.raph) text = TURTLE_DETAILS[3]; } 

我无法解决问题

 int id = intent.getIntExtra("turtle_id", R.id.leo); 

我不明白为什么指定了R.id.leoturtle_id是名字,但我不确定R.id.leo

MainActivity.java片段

  /* * Called when the Details activity finishes running and comes back to here. */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); } /* * Called when the user clicks on the large TMNT image button. * Loads the DetailsActivity for more information about that turtle. */ public void onClickTurtleImage(View view) { Intent intent = new Intent(this, DetailsActivity.class); RadioGroup group = (RadioGroup) findViewById(R.id.turtle_group); int id = group.getCheckedRadioButtonId(); intent.putExtra("turtle_id", id); startActivity(intent); } /* * This method is called when the user chooses one of the turtle radio buttons. * In this code we set which turtle image is visible on the screen in the ImageView. */ public void pickTurtle(View view) { ImageButton img = (ImageButton) findViewById(R.id.turtle); if (view.getId() == R.id.leo) { img.setImageResource(R.drawable.tmntleo); } else if (view.getId() == R.id.mike) { img.setImageResource(R.drawable.tmntmike); } else if (view.getId() == R.id.don) { img.setImageResource(R.drawable.tmntdon); } else if (view.getId() == R.id.raph) { img.setImageResource(R.drawable.tmntraph); } } } 

DetailsActivity.java

 /* * CS 193A, Winter 2015, Marty Stepp * This app is a continuation of our TMNT app from last week. * Today's version adds a second activity and launches that activity using an Intent. * This file represents the Java code for the second activity. */ package com.example.stepp.layoutfun; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class DetailsActivity extends Activity { /* * Constant array of data about each of the four turtles. * (This is not the most idiomatic way to store such information, * but we'll come back to it later.) */ private static final String[] TURTLE_DETAILS = { ""/*Long Story but not relevant for the question*/ }; /* * Called when the activity first gets created. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); // pull the turtle's ID out of the intent that the MainActivity used to load me Intent intent = getIntent(); int id = intent.getIntExtra("turtle_id", R.id.leo); String text = ""; if (id == R.id.leo) { text = TURTLE_DETAILS[0]; } else if (id == R.id.mike) { text = TURTLE_DETAILS[1]; } else if (id == R.id.don) { text = TURTLE_DETAILS[2]; } else { // if (id == R.id.raph) text = TURTLE_DETAILS[3]; } TextView tv = (TextView) findViewById(R.id.turtle_info); tv.setText(text); } } 

activity_main.xml

          

使用过的教程在这里 。

getIntExtra方法的文档如下:

从intent中检索扩展数据。

参数

name所需项目的名称。

defaultValue如果没有使用给定名称存储所需类型的值,则返回该值。

返回

先前使用putExtra()添加的项的值,如果未找到,则为默认值。

因此,在您的示例中,如果该密钥存在于Intent ,则将为id分配与密钥turtle_id关联的整数值,否则将为其分配整数值R.id.leo 。 通常,如果消费者在启动此Activity时未能传递必要信息,则使用此方法以便提供合理的默认值。 在您的特定情况下,此行为可以解释为:“如果调用者忘记告诉我在启动此Activity时选择了哪只乌龟,则假设它是Leo。”

实际上@stkent的答案是正确的。 如果在"turtle_id"下找不到任何值,则右参数就像备份一样。 此外,作为一般信息来源,请查看有关您问题的Android API 。 在许多情况下,你会发现一个简短而真实的答案,这些方法是如何运作的。