我想我没有正确描述我在question part 1中的意思。所以我在这里输入更多细节。请看一看 。
activityOne.java
EditText get_input;
Button get_button;
String display;
int image_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
get_input=(EditText)findViewById(R.id.input_year);
get_button=(Button)findViewById(R.id.button_submit);
get_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int store_get_input=Integer.parseInt(get_input.getText().toString());
int results;
results=store_get_input%12;
if (results == 0) {
display = "Your number is "store_get_input";
image_id=0;
} else if (result ==1){
display = "Your number is "store_get_input";
image_id=1;
}else{
display = "Your number is others";
image_id=3;
}
Intent home_intent=new Intent(getApplicationContext(),Result_page.class);
home_intent.putExtra("home_store_get_input",display);
home_intent.putExtra("home_store_image_src",image_id);
startActivity(home_intent);
}
});
}
activityTwo.java
public class Result_page extends AppCompatActivity {
TextView get_result;
ImageView get_image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result_page);
get_result=(TextView)findViewById(R.id.display_result);
get_image=(ImageView)findViewById(R.id.image_view);
Intent result_intent=getIntent();
String text=result_intent.getStringExtra("home_store_get_input");
get_result.setText(text);// display what the user has enter
// get_image.setImageResource
//OR other coding to display imageview by using the image_id from activty 1?
}
从两个java文件中,我认为你们阅读的内容会更清晰,更容易。所以我坚持使用activityTwo.java,我需要键入一些代码,以便通过uising int image_id或其他编码来显示imageview。
答案 0 :(得分:0)
在第二个活动中将字符串转换为整数,并使用开关案例根据该数字选择图像。喜欢:
Intent result_intent = getIntent();
String text = result_intent.getStringExtra("home_store_get_input");
try {
int number = Integer.parseInt(text);
} catch(NumberFormatException e) {
//Exception
}
int imageId = R.Drawable.default_image;
switch(number) {
case 1:
imageId = R.drawable.image1;
break;
case 2:
imageId = R.drawable.image2;
break;
........so on
}
ImageView.setImageResource(imageId);
答案 1 :(得分:0)
我认为你不需要2个重复的问题。
方法1: https://stackoverflow.com/a/38342798/2670771
方法2: 在活动1中,只需将图片ID发送到活动2
if (results == 0) {
display = "Your number is "store_get_input";
image_id = R.drawable.image0;
} else if {...}
在活动2中,从意图中获取图像ID
int defaultIdWhenCouldNotFindImageSourceId = -1;//should use a negative value
int imageId = result_intent.getIntExtra("home_store_image_src", defaultIdWhenCouldNotFindImageSourceId);
if( defaultIdWhenCouldNotFindImageSourceId != imageId) {
//when you can find an valid image id in intent extra, display it
get_image.setImageResource(imageId);
}