将值传递给另一个类

时间:2023-01-16 17:02:11

I'm working on an app that has a joystick on the MainActivity. The joystick is a custom view and has a Joystick class. When the user touches the joystick and moves it, I want the Y-Axis value to be sent to the MainActivity so that I can work with the value.

我正在开发一个在MainActivity上有操纵杆的应用程序。操纵杆是自定义视图,并具有Joystick类。当用户触摸操纵杆并移动它时,我希望将Y轴值发送到MainActivity,以便我可以使用该值。

In the Joystick.java, I have an onTouch() listener. When touched, the joystick produces the coordinates (the X and Y of the joystick) so that I can get a value from -100 to 100 on the Y-Axis.

在Joystick.java中,我有一个onTouch()监听器。触摸时,操纵杆会生成坐标(操纵杆的X和Y),这样我就可以在Y轴上获得-100到100的值。

public class Joystick extends View implements OnTouchListener{
    int xAxis, yAxis;

    public int getYAxis(){
        return yAxis;
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent event){
        // Code to process event and assign xAxis and yAxis values (-100, 100)
    }
}

In the MainActivity I tried to do the following to get the yAxis value:

在MainActivity中,我尝试执行以下操作以获取yAxis值:

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // ... other code

    Joystick joy = (Joystick) findViewById(R.id.joystick1);
    joy.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
            // updateMotorControl() is the funcion I need to pass my value to
            updateMotorControl(joy.getYAxis(), 0);

            return true;
        }
    }

But of course AndroidStudio tells me that joy needs to be declared final. The problem with that is that if it's final, I can never update the value.

但是当然AndroidStudio告诉我,欢乐需要被宣布为最终。问题是如果它是最终的,我永远不能更新该值。

I've tried a million things, and I keep getting similar errors. I'm not asking why I get that error. What I want to know is what's the best way to pass that value to the MainActivity and in turn to updateMotorControl()?

我已经尝试了一百万件事情,并且我一直遇到类似的错误。我不是在问为什么我会收到这个错误。我想知道的是,将该值传递给MainActivity并将其传递给updateMotorControl()的最佳方法是什么?

3 个解决方案

#1


you should create a new interface

你应该创建一个新的界面

public interface DataCallback {
        /**
         * This method is called when xAxis and yAxis change.
         * @param instance
         */
        void dataIsAvailableNow(int x, int y);
    }

in the Joystick add a DataCallback field:

在操纵杆中添加一个DataCallback字段:

public class Joystick extends View implements OnTouchListener{
        int xAxis, yAxis;
        DataCallback mCallback;

        public int getYAxis(){
            return yAxis;
        }

        public void setCallback(DataCallback callback) {
            this.mCallback = callback;
        }

        @Override
        public boolean onTouch(View arg0, MotionEvent event){
            // Code to process event and assign xAxis and yAxis values (-100, 100)
            if (mCallback != null) {
                mCallback.dataIsAvailableNow(-100,100);
            }
        }
    }

and in the MainActivity implement the interface and set the listener

并在MainActivity中实现接口并设置监听器

joy.setCallback(this);

#2


Go ahead and make joy final. 'final' doesn't mean you can't change the values inside your object; it only means you can't replace the object itself. Recall that Java passes around objects as their reference value - making joystick final just means you can't replace it with another instance.

来吧,让最后的喜悦。 'final'并不意味着你无法改变对象内的值;它只意味着你不能替换对象本身。回想一下,Java传递对象作为它们的参考值 - 使操纵杆最终只是意味着你不能用另一个实例替换它。

#3


You could implement a callback function.

您可以实现回调函数。

See here: Callback functions in Java and here: How do I perform a JAVA callback between classes?

请参阅此处:Java中的回调函数和此处:如何在类之间执行JAVA回调?

#1


you should create a new interface

你应该创建一个新的界面

public interface DataCallback {
        /**
         * This method is called when xAxis and yAxis change.
         * @param instance
         */
        void dataIsAvailableNow(int x, int y);
    }

in the Joystick add a DataCallback field:

在操纵杆中添加一个DataCallback字段:

public class Joystick extends View implements OnTouchListener{
        int xAxis, yAxis;
        DataCallback mCallback;

        public int getYAxis(){
            return yAxis;
        }

        public void setCallback(DataCallback callback) {
            this.mCallback = callback;
        }

        @Override
        public boolean onTouch(View arg0, MotionEvent event){
            // Code to process event and assign xAxis and yAxis values (-100, 100)
            if (mCallback != null) {
                mCallback.dataIsAvailableNow(-100,100);
            }
        }
    }

and in the MainActivity implement the interface and set the listener

并在MainActivity中实现接口并设置监听器

joy.setCallback(this);

#2


Go ahead and make joy final. 'final' doesn't mean you can't change the values inside your object; it only means you can't replace the object itself. Recall that Java passes around objects as their reference value - making joystick final just means you can't replace it with another instance.

来吧,让最后的喜悦。 'final'并不意味着你无法改变对象内的值;它只意味着你不能替换对象本身。回想一下,Java传递对象作为它们的参考值 - 使操纵杆最终只是意味着你不能用另一个实例替换它。

#3


You could implement a callback function.

您可以实现回调函数。

See here: Callback functions in Java and here: How do I perform a JAVA callback between classes?

请参阅此处:Java中的回调函数和此处:如何在类之间执行JAVA回调?