У меня есть страница с 6 кнопками изображения, когда пользователь нажимает на каждый из них, перейдя на страницу сведений, которая содержит изображение этого значка с дополнительной информацией. Вопрос в том, как настроить изображение на странице подробностей, соответствует кнопке изображения, которую пользователь нажал? например, когда пользователь нажал кнопку 1 изображения на странице подробных сведений, нажмите кнопку изображения1, когда нажата кнопка изображения 2 на странице с подробной информацией у нас есть изображение кнопки2, как я могу определить это свойство в коде страницы активности?Как установить кнопку изображения во вложенном действии?
вот мой код, соответствующий 6 кнопок изображения:
// реализовать интерфейс OnClickListener общественного класса назначения расширяет 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);
}
}
}
спасибо, но я не понял, куда я должен поместить код на страницу DestinationDetail? –
Второй фрагмент находится в onCreate() DestinationDetail. Измените свои идентификаторы соответственно –
PS: Я не вижу вызова setContentView() там ... вы должны иметь его, иначе ваша иерархия представлений не будет привязана к окну, и вы ничего не увидите –