私はすべてのアクティビティでいくつかのコードを実装しようとしており、コードをコピーして各アクティビティにページしたくありません。 インターフェイスを使用してすべてのアクティビティでコードを実装する
は、もともと私はその後、他のすべてを延長コードで親アクティビティを持っていたが、私はListActivitiesまたはExpandableListActivitiesでこれを行うことができませんでした。これはインターフェイスクラスを使用し、各アクティビティにこれを実装させることで実現されると思います。しかし、私がこの試みをしようとすると、私にエラーが表示され、メソッド本体が削除されます。ここで
は、私はJavaでこれまで
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;
}
}
すてきな質問、私は興味深い答えを知りたいです。 –