如何使用2个公共类,在相同片段中creat函数上受保护的void?

问题描述 投票:0回答:1
  1. 如何在Android Studio的MainActivity java中放置多个类,概述,空缺?我需要同时具有按钮和代码,以使Google地图在同一页面上具有某些限制。应该更改类的什么名称才能起作用。这些代码在独立放置但不能一起放置时有效。如何使它们协同工作?

    
         package com.example.maps;
         import android.content.Intent;
         import android.os.Bundle;
         import android.support.v4.app.FragmentActivity;
         import android.support.v7.app.AppCompatActivity;
         import android.view.View;
         import android.widget.Button;
    
         import com.google.android.gms.maps.CameraUpdateFactory;
         import com.google.android.gms.maps.GoogleMap;
         import com.google.android.gms.maps.OnMapReadyCallback;
         import com.google.android.gms.maps.SupportMapFragment;
         import com.google.android.gms.maps.model.LatLng;
         import com.google.android.gms.maps.model.LatLngBounds;
         import com.google.android.gms.maps.model.MarkerOptions;
    
    
         public class FirstFragment extends AppCompatActivity {
            Button button;
            @Override
           protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
              button = (Button) findViewById(R.id.button);
              button.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View v) {
                       openFoodFragment();
                    }
               });
            }
           public void openFoodFragment(){
                Intent intent = new Intent(this, FoodFragment.class);
                startActivity(intent);
            }
         }
        public class MainACTIVITY extends FragmentActivity implements OnMapReadyCallback {
    
            GoogleMap mapAPI;
           SupportMapFragment mapFragment;
           @Override
            protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_main);
               mapFragment =(SupportMapFragment)
                       getSupportFragmentManager().findFragmentById(R.id.mapAPI);
    
                mapFragment.getMapAsync(this);
            }
            public void openFoodFragment(){
               Intent intent = new Intent(this, FoodFragment.class);
               startActivity(intent);
            }
          @Override
           public void onMapReady(GoogleMap googleMap){
              mapAPI =  googleMap ;
    
              LatLng one = new LatLng(-21.754812, -48.219451);
              LatLng two = new LatLng(-21.787443, -48.113332);
    
             LatLngBounds.Builder builder = new LatLngBounds.Builder();
    
             //add them to builder
             builder.include(one);
             builder.include(two);
    
             LatLngBounds bounds = builder.build();
    
             //get width and height to current display screen
             int width = getResources().getDisplayMetrics().widthPixels;
             int height = getResources().getDisplayMetrics().heightPixels;
             int padding = (int) (width * 0.20);
             mapAPI.setLatLngBoundsForCameraTarget(bounds);
              mapAPI.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding));
    
              //set zoom to level to current so that you won't be able to zoom out viz. move outside       bounds
             mapAPI.setMinZoomPreference(mapAPI.getCameraPosition().zoom);
    
    
             LatLng Kampai = new LatLng(-21.780985, -48.186859);
             mapAPI.addMarker(new MarkerOptions().position(Kampai).title("Kampai"));
             mapAPI.moveCamera(CameraUpdateFactory.newLatLng(Kampai));
    
             }
    
             }  
    
android-studio class void protected simultaneous
1个回答
0
投票

是的,可以将它们放在同一个文件中,但是您的mainACTIVITY必须位于FirstFragment内,而这不会发生,您需要移除最底部的括号并将其放在mainACTIVITY类的开头的正上方,在粘贴之前,先关闭FirstFragment的括号另一个类放入文件。

© www.soinside.com 2019 - 2024. All rights reserved.