CheckBoxPreferences的值只有在Activity重新启动后才会生效



我有一个关于CheckBoxPreferences的问题。我已经阅读了一些相关的线程,并尝试了其中的一些,但作为一个初学者,我仍然在努力寻找我所面临的问题的解决方案。下面的代码是在google地图上应用一个多边形,通常它是这样做的:

public class ZurichGreifensee extends Activity implements OnClickListener {
    String latLngUster = "lat=47.348491&lon=8.717529";
    private final LatLng LOCATION_GREIFENSEE = new LatLng(47.348124, 8.681688);
    private GoogleMap mMap;
    Button fishActivity;
    ToggleButton mapView;
    Polygon Naturschutzgebiet, Seeschutzzone;
    CheckBoxPreference naturschutzgebiet;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.lakes_info);
        NetworkConnectivityCheck checkit = new NetworkConnectivityCheck();
        Boolean conn = checkit.checkNow(this.getApplicationContext());
        if(conn == true){
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        CameraUpdate updateZurichGreifensee = CameraUpdateFactory.newLatLngZoom(LOCATION_GREIFENSEE, 13);
        mMap.animateCamera(updateZurichGreifensee);
        mMap.setMyLocationEnabled(true);
        mapView = (ToggleButton) findViewById(R.id.tbSatNorMapView);
        fishActivity = (Button) findViewById(R.id.bWhereAmI);
        mapView.setOnClickListener(this);
        fishActivity.setOnClickListener(this);
        }else{
            connectivityMessage("Kein Netzwerk");
        }
        SharedPreferences getGewasserRevierKarte = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean naturschutzgebiet = getGewasserRevierKarte.getBoolean("naturschutzgebiet", false);{
            if (naturschutzgebiet == true){
                addNaturschutzgebiet();}
        }
        boolean seeschutzzone_1 = getGewasserRevierKarte.getBoolean("seeschutzzone_1", false);{
            if (seeschutzzone_1 == true)
                addSeeschutzzone();
        }
    }
    private void connectivityMessage(String string) {
        // TODO Auto-generated method stub
        Context context = getApplicationContext();
        Toast toast = Toast.makeText(context, "", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setText(string);
        toast.show();
    }

    private void addNaturschutzgebiet() {
        Naturschutzgebiet = mMap.addPolygon(new PolygonOptions()
            .add(new LatLng(47.366676,8.673754), new LatLng(47.367526,8.67236), new LatLng(47.369459,8.670965), new LatLng(47.371886,8.669538),
                 new LatLng(47.372525,8.669184), new LatLng(47.375453,8.661481), new LatLng(47.376819,8.657801), new LatLng(47.378032,8.652318), 
                 new LatLng(47.375962,8.651159), new LatLng(47.378839,8.646846), new LatLng(47.378759,8.646739), new LatLng(47.375562,8.651449),  
                 new LatLng(47.37432,8.650172), new LatLng(47.37291,8.652007), new LatLng(47.371639,8.650355), new LatLng(47.370403,8.650634),
                 new LatLng(47.368972,8.650859), new LatLng(47.368071,8.652973), new LatLng(47.368173,8.653337), new LatLng(47.368122,8.653788),
                 new LatLng(47.367904,8.654228), new LatLng(47.367947,8.654593), new LatLng(47.369829,8.653616), new LatLng(47.371057,8.653488), 
                 new LatLng(47.372155,8.65588), new LatLng(47.372656,8.657597), new LatLng(47.373608,8.661545), new LatLng(47.37262,8.663927), 
                 new LatLng(47.371145,8.666051), new LatLng(47.370098,8.667457), new LatLng(47.370055,8.668186), new LatLng(47.369386,8.66839), 
                 new LatLng(47.369241,8.669044), new LatLng(47.367497,8.67074), new LatLng(47.367243,8.670557), new LatLng(47.367293,8.670836), 
                 new LatLng(47.366792,8.671544), new LatLng(47.366836,8.671823), new LatLng(47.365935,8.672746))
                    .strokeWidth(2)
                    .strokeColor(Color.RED)
                    .fillColor(Color.argb(64, 255, 0, 0))); 
        }
    private void addSeeschutzzone() {
        Seeschutzzone = mMap.addPolygon(new PolygonOptions()
            .add(new LatLng(47.367947,8.654593), new LatLng(47.369829,8.653616), new LatLng(47.371057,8.653488), 
                 new LatLng(47.372155,8.65588), new LatLng(47.372656,8.657597), new LatLng(47.373608,8.661545), new LatLng(47.37262,8.663927),
                 new LatLng(47.371145,8.666051), new LatLng(47.370098,8.667457), new LatLng(47.370055,8.668186), new LatLng(47.369386,8.66839), new LatLng(47.369241,8.669044), 
                 new LatLng(47.368187,8.670085), new LatLng(47.368412,8.669001), new LatLng(47.36964,8.667097), new LatLng(47.370715,8.665166),
                 new LatLng(47.371442,8.663599), new LatLng(47.371413,8.661239), new LatLng(47.371006,8.658149), new LatLng(47.369989,8.656261))
                     .strokeWidth(2)
                     .strokeColor(Color.MAGENTA)
                     .fillColor(Color.argb(64, 170, 0, 120)));
    }

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) { 
                case R.id.bWhereAmI:
                    String locationZurichGreifensee = latLngUster.toString();
                    Bundle basketZurich = new Bundle();
                    basketZurich.putString("location", locationZurichGreifensee);
                    Intent t = new Intent(ZurichGreifensee.this, MainActivity.class);
                    t.putExtras(basketZurich);
                    startActivity(t);
                    break;
                }
                //Switching between normal and hybrid view
                if (mapView.isChecked()) {
                    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                }else {
                    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                }   
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);
        MenuInflater gewasserMenu = getMenuInflater();
        gewasserMenu.inflate(R.menu.gewasserkarte, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
        case R.id.gewasserkarte:
            Intent gewasser_revier_karte = new Intent(ZurichGreifensee.this, GewasserRevierKarte.class);
            startActivity(gewasser_revier_karte);
            break;
        }
        return false;
    }   
}

这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <CheckBoxPreference
        android:key="naturschutzgebiet"
        android:title="Naturschutzgebiet"
        android:summaryOn="Wege nicht verlassen, Hunde an der Leine führen"
        android:icon="@drawable/naturschutzgebiet"
        android:defaultValue="false" />
    <CheckBoxPreference
        android:key="seeschutzzone_1"
        android:title="Seeschutzzone"
        android:summaryOn="Befahren mit Booten oder anderen Schwimmkörpern sowie Baden ganzjährig verboten"
        android:icon="@drawable/seeschutzzone_1"
        android:defaultValue="false" />
</PreferenceScreen>

要在地图上显示上述定义的多边形之一,需要在首选项菜单中勾选相应的复选框。现在,即使我这样做,多边形是不可见的,在我关闭首选项菜单。相反,我必须通过关闭并重新启动ZurichGreifensee活动来重新启动它。只有这样,代码才会在地图上应用多边形。

我的问题是,我需要在上面的代码中更改/添加什么,以便在我勾选所需的首选项复选框并关闭/退出首选项菜单后立即显示多边形?

提前感谢。

你需要重写onResume方法并检查用户是否按下了那里的复选框

@Override
public void onResume () {
    super.onResume();
    //if user has checked the box
    //is rectangle isn't already drawn
    //draw rectangle
}

相关内容

  • 没有找到相关文章

最新更新