我有一个带有6个图像按钮的页面,当用户点击每个图像按钮时,将转到详细页面,其中包含带有额外信息的图标的图像。问题是如何在详细页面中设置图像对应于用户单击的图像按钮?例如,当用户点击详细页面中的图像按钮1时,在单击详细信息页面中的图像按钮2时看到图像按钮1,我们有button2的图像,如何在活动页面代码中定义此属性?
这是我的代码对应于6个图像按钮:
//实现OnClickListener接口 public class Destination扩展ActionBarActivity 实现View.OnClickListener
{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.destination);
//get the Button reference
//Button is a subclass of View
//buttonClick if from main.xml "@+id/buttonClick"
View btnClick = findViewById(R.id.image1);
View btnClick2 = findViewById(R.id.image2);
View btnClick3 = findViewById(R.id.image3);
View btnClick4 = findViewById(R.id.image4);
View btnClick5 = findViewById(R.id.image5);
View btnClick6 = findViewById(R.id.image6);
btnClick.setOnClickListener(this);
btnClick2.setOnClickListener(this);
btnClick3.setOnClickListener(this);
btnClick4.setOnClickListener(this);
btnClick5.setOnClickListener(this);
btnClick6.setOnClickListener(this);
}
//override the OnClickListener interface method
@Override
public void onClick(View arg0) {
if (arg0.getId() == R.id.image1) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, DestinationTherapy.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image2) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image3) {
//define a new Intent for the second Activity
Intent intent = new Intent(Destination.this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image4) {
//define a new Intent for the second Activity
Intent intent = new Intent(Destination.this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image5) {
//define a new Intent for the second Activity
Intent intent = new Intent(Destination.this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image6) {
//define a new Intent for the second Activity
Intent intent = new Intent(Destination.this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(this, Setting.class));
return true;
} else if (id == R.id.action_menu) {
startActivity(new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
目标详情活动:
public class DestinationDetail extends ActionBarActivity
implements View.OnClickListener
{
View btnClick = findViewById(R.id.emergency4);
btnClick.setOnClickListener(DestinationDetail.this);
View btnClick1 = findViewById(R.id.back2);
btnClick1.setOnClickListener(DestinationDetail.this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingActivity.class));
return true;
}
if (id == R.id.action_menu) {
startActivity(new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View arg0) {
if (arg0.getId() == R.id.emergency4) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, EmergencyCall.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.back2) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, Destination.class);
//start the second Activity
this.startActivity(intent);
}
}
}
答案 0 :(得分:0)
Intent
在Destination
活动中,在onClick()
中写下如下内容:
public void onClick(View view) {
int id = view.getId();
// ...
if (id == R.id.image1) {
Intent intent = new Intent(this, DestinationTherapy.class);
intent.putExtra("res_id", R.drawable.image1);
startActivity(intent);
} else if ( //...
}
将R.drawable.image1
更改为相应的res ID。在你的情况下,每个图像都会有所不同,所以你需要一种if / switch结构。
在DestinationDetail
中获取图像的句柄并将其设置为
setContentView(...);
ImageView iv = (ImageView) findViewById(R.id.my_detail_image_view);
int resId = getIntent().getIntExtra("res_id", 0);
iv.setImageResource(resId);
您必须使用R.id.my_detail_image_view
DestinationDetail
澄清(view id vs res id):视图ID是您在xml中定义的android:id="@+id/my_view_id"
。将图像放入可绘制文件夹时,会自动为您生成图像的res id。您可以在带有R.id.image_name_without_extension
的java或带有@drawable/image_name_without_extension