Я пытаюсь внедрить некоторый код для каждого действия и не хочу копировать и переводить код в каждое действие.Использование интерфейса для реализации кода во всех действиях
Первоначально у меня была родительская активность с кодом, а затем расширены все остальные, но я не мог этого сделать в ListActivities или ExpandableListActivities.
Я думаю, что это будет сделано с использованием класса интерфейса, после чего каждый из них реализует это. Однако, когда я пытаюсь сделать это, Eclipse дает мне ошибку и говорит, чтобы удалить тело метода.
Вот то, что я до сих пор
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public interface MenuOptions {
/**
* Method called when the hardware menu button is called. Uses optionmenu.xml for layout
*/
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionmenu, menu);
return true;
}
/**
* Event listener for the options menu. If home is pressed user is sent to home screen. If settings is pressed user is sent to setting screen
* User is passed as an extra
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent nextIntent = null;
switch (item.getItemId()) {
case R.id.home:
Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
nextIntent = new Intent(this, Home.class);
break;
case R.id.settings:
Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show();
nextIntent = new Intent(this, Settings.class);
break;
}
nextIntent.putExtra("user", user);
startActivity(nextIntent);
return true;
}
}
Хороший вопрос, я бы хотел узнать интересные ответы. –