当前位置: 首页 > 知识库问答 >
问题:

聆听偏好变化并将其应用于android studio中的另一项活动

蓬兴国
2023-03-14

我有一个谷歌地图片段,在我的MainActive和一个SettingsActive,它有一个ListPreance选项来为地图选择样式。我想做的是倾听该首选项的变化,并相应地更改MainActive上的地图。我只能在onMap就绪上更改它,这是一个一次性的事情,它不是一个侦听器。我如何才能做到这一点?我试图在SettingsActive中做到这一点,但我不能访问mapStyle

主要活动:

    private MapFragment mapFragment;
    String mapStyle;
    private GoogleMap map;
    private GoogleApiClient googleApiClient;
    SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        mapStyle = prefs.getString("list_preference_1", "<unset>");

        mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        if (googleApiClient == null) {
                    googleApiClient = new GoogleApiClient.Builder(this)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .addApi(LocationServices.API)
                            .build();
                }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.setOnMapClickListener(this);
        map.setOnMarkerClickListener(this);
        map.getUiSettings().setZoomControlsEnabled(true);

        try {
            // Customise the styling of the base map using a JSON object defined
            // in a raw resource file.
            int res;
            if (mapStyle.equals("Silver")){
                res = R.raw.silver_style;
            } else if (mapStyle.equals("Retro")) {
                res = R.raw.retro_style;
            } else if (mapStyle.equals("Dark")) {
                res = R.raw.dark_style;
            } else if (mapStyle.equals("Night")) {
                res = R.raw.night_style;
            } else if (mapStyle.equals("Aubergine")) {
                res = R.raw.aubergine_style;
            } else {
                res = R.raw.standard;
            }
            boolean success = googleMap.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(
                            this, res));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }
    }

设置活动:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }

共有1个答案

商勇
2023-03-14

也许不是最好的解决方案,但我认为您可以在活动恢复上更新您的地图样式。打开设置活动并选择样式后,您可以返回地图活动。OnResume函数将被调用,您将检查选择了哪种样式,并根据此信息更新地图。

也许这样就足够了:

@Override
protected void onResume() {
    super.onResume();
    SetMapStyle(map);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    map = googleMap;
    map.setOnMapClickListener(this);
    map.setOnMarkerClickListener(this);
    map.getUiSettings().setZoomControlsEnabled(true);
    SetMapStyle(googleMap);
}

private void SetMapStyle(GoogleMap googleMap){

    if(googleMap == null)
        return;

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    mapStyle = prefs.getString("list_preference_1", "<unset>");

    try {
        
        int res;
        if (mapStyle.equals("Silver")){
            res = R.raw.silver_style;
        } else if (mapStyle.equals("Retro")) {
            res = R.raw.retro_style;
        } else if (mapStyle.equals("Dark")) {
            res = R.raw.dark_style;
        } else if (mapStyle.equals("Night")) {
            res = R.raw.night_style;
        } else if (mapStyle.equals("Aubergine")) {
            res = R.raw.aubergine_style;
        } else {
            res = R.raw.standard;
        }
        boolean success = googleMap.setMapStyle(
                MapStyleOptions.loadRawResourceStyle(
                        this, res));

        if (!success) {
            Log.e(TAG, "Style parsing failed.");
        }
    } catch (Resources.NotFoundException e) {
        Log.e(TAG, "Can't find style. Error: ", e);
    }

}
 类似资料:
  • 问题内容: 我正在尝试将图像从一项活动发送到另一项活动,但我不知道如何设置imageview。 这是我发送图像和其他内容的方式 这是我尝试读取图像并将其设置为ImageView的方法,但是出现语法错误。 我应该如何设置ImageView? 问题答案: 你不觉得 应该

  • 我在这个问题上也有同样的问题 如何使用android中的回收器视图将事件放在日历中 //Event.java 我尝试了很多,但它甚至不工作。我的事件。我要在另一个适配器中使用java。也许在onBindViewHolder中。

  • 聆听网上电台 1. 进入(网络)的(网上电台),选择要使用的网上电台播放器。 若网上电台播放器被登录至Memory Stick™,需先将Memory Stick™正确插入PSP™内。 与网络联机。 2. 显示「确定要执行此网页中的Plug-In吗?」的讯息后选择「是」。 显示网上电台播放器画面后,即可开始聆听网上电台。 提示 要使用网上电台时,需与无线基地台联机。 操作方法与按钮的设计配置等可能因

  • 问题内容: 在我的应用程序中,我需要将Uri图像从我的第一个Activity转移到另一个。我知道如何通过意图发送位图。我是一名bigginer程序员,所以我不知道会做些什么:有意图地传输Uri或将Uri更改为位图然后发送? 问题答案: 与putExtra一起使用以发送Uri路径: 在newActivity OnCreate方法中: 使用这些func:Uri到String: 字符串到Uri:

  • 我的kotlin项目有两个activitis(main,第二个名为:AddPlayer)。main活动有一个名为“添加播放器”的按钮,它启动第二个活动。在第二个活动中,我有两个编辑文本来获得一个数字和一个来自用户的文本。在第二个活动中按下提交按钮后,在主活动中,我想将这些值添加到我的可变列表中,但idk如何操作(我认为它崩溃了,因为主活动在从用户获取值之前使用了这些变量)plz指导我。 我的活动代