- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
@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.leo
? turtle_id
是名称,但我不确定 R.id.leo
的意义。
MainActivity.java
的 fragment
/*
* 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
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:gravity="top|center"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<RadioGroup
android:id="@+id/turtle_group"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/leo"
android:onClick="pickTurtle"
android:text="Leo"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/mike"
android:onClick="pickTurtle"
android:text="Mike"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/don"
android:onClick="pickTurtle"
android:text="Don"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/raph"
android:onClick="pickTurtle"
android:text="Raph"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<ImageButton
android:id="@+id/turtle"
android:onClick="onClickTurtleImage"
android:src="@drawable/tmntleo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
使用的教程是here .
最佳答案
documentation for the getIntExtra
method如下:
Retrieve extended data from the intent.
Parameters
name
The name of the desired item.
defaultValue
the value to be returned if no value of the desired type is stored with the given name.Returns
the value of an item that previously added with putExtra() or the default value if none was found.
因此,在您的示例中,如果 Intent
中存在该键,则 id
将被分配与键 turtle_id
关联的整数值,并且如果不是,将被分配整数值 R.id.leo
。通常,如果消费者在启动此 Activity
时未能传递必要的信息,则使用它来提供合理的默认值。在您的特定情况下,此行为可以解释为:“如果调用者在启动此 Activity
时忘记告诉我选择了哪只乌龟,则假设它是 Leo。”
关于java - 了解 getIntExtra() 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34110470/
请帮帮我,我不明白这有什么问题。被调用 Activity 中的 k 值始终为 1。 我的通话 Activity 代码 pos_st = position;
我正在尝试使用 Intent 在 Activity 之间传递一个整数。源 Activity 进行调用 info.id 是从中选择的项目一个 ListView 。 Intent intent = new
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState
我读过 Hello Android 一书,但我不明白以下代码。我不知道如何处理 getIntExtra() 和 putExtra() int 这段代码。 private void startGame
我在让它工作时遇到问题,这就是我想要达到的目标:一个 onClickListener,它将获取 radio 的状态并返回 toast。但是我在某个地方搞砸了。我对 Intent 有疑问,任何人都可以对
我有这个调用 Activity 的代码 Intent intent; intent=new Intent(TopicsActivity.this, Di
int countervalue = i.getIntExtra("Count", 10); 我在不同的 Activity 中有这行代码,当有人点击按钮时,他们会转到该 Activity 。如果没有这
如何从 onResume() 的重写中调用 getIntExtra? 错误代码如下: @Override protected void onResume() { super.onResume
我很困惑: intent.getExtras.getInt() 和 intent.getIntExtra() 一样吗? 如果我使用 START_REDELIVER_INTENT 开始我的服务,额外的会
我是一名优秀的程序员,十分优秀!