java - 使用接口(interface)实现所有 Activity 的代码

标签 java android

我正在尝试在每个 Activity 上实现一些代码,不想将代码复制并分页到每个 Activity 中。

最初,我只有一个父 Activity 和代码,然后扩展了所有其他 Activity ,但我无法在 ListActivities 或 ExpandableListActivities 上执行此操作。

我认为这将通过使用接口(interface)类然后让每个 Activity 实现它来完成。然而,当我尝试这样做时,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;
    }

}

最佳答案

Java 中的

Interface 类应该只包含方法签名而不包含实现。因此你必须创建一个基类:

public class MenuOptions extends Activity {
  /**
     * 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;
    }
}

以及您的 Activity :

public class YourActivity extends MenuOptions {
/*...*/
}

关于java - 使用接口(interface)实现所有 Activity 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8521079/

相关文章:

java - 使用 buildpack 中的 SQL 驱动程序在 Pivotal Cloud Foundry 上部署具有多个数据源的应用程序

java - 如何从 JdbcTemplate 在 MySQL 中创建用户?

java - 如何通过其uid获取Firestore用户列表?

android - Drawable setState 返回 true 但不改变外观

android - 具有多个 ArrayLists 的 Sticky Header RecyclerView

java - 如何在 hadoop map-reduce 作业中创建文件?

java - Spring MVC - Servlet 调度程序抛出异常 : java. lang.StackOverflowError

java - 不幸的是,Myapp在输入登录详细信息后已停止

java - LibGDX 相机旋转问题

java - 处理列表中对象的属性?