如何在 Activity 和视图 class 之间传输数据?

How to transfer data between an Activity and a View class?

我想将我的 MainActivity class 中的 2 个 float 值和 1 个 boolean 值转移到 MyCanvas class(即class 扩展视图)?这可能吗?

我知道这是一个新手问题,但我发现的所有内容都告诉我要使用 IntentBundle,或者只使用 Bundle.setArguments(),但显然,none他们中的 View class.

谢谢。

编辑 1

这是我的MainActivity

public class MainActivity extends AppCompatActivity {

    private MyCanvas myCanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myCanvas = (MyCanvas) findViewById(R.id.canvas);
    }

    public void btnCalcularOnClick(View v) {

        TextView xResultado = (TextView) findViewById(R.id.xResultado);
        TextView yResultado = (TextView) findViewById(R.id.yResultado);

        EditText txtX = (EditText) findViewById(R.id.txtX);
        EditText txtY = (EditText) findViewById(R.id.txtY);

        //Comeco da Matematica

        float x = Float.parseFloat(txtX.getText().toString());
        float y = Float.parseFloat(txtY.getText().toString());

        float xResult = 5 * x;
        float yResult = 35 * y;

        boolean buttonState = true;
    }
}

MyCanvas class就是这样

public class MyCanvas extends View {
    Paint myPaint;

    public MyCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
        myPaint = new Paint();
    }

    @Override
    public void onDraw(Canvas myCanvas) {
        super.onDraw(myCanvas);
        myPaint.setColor(Color.BLACK);
        myPaint.setStrokeWidth(3);

        float cx, cy;
        boolean buttonState2;
    }
}

在这种情况下,我要转账:

您可以使用 settersMyCanvas class 中设置所需的值。

像这样在 MyCanvas class 中创建方法。

public class MyCanvas extends View {

    private float cx, cy;
    private boolean buttonState2;

    ...

    public void setResults(float xResult, float yResult) {
        cx = xResult;
        cy = yResult;
    }

    public void setButtonState(boolean state) {
        buttonState2 = state;
    }
}

然后在你的activityclass

public class MainActivity extends AppCompatActivity {

    private MyCanvas myCanvas;

    ...

    public void btnCalcularOnClick(View v){

        TextView xResultado = (TextView)findViewById(R.id.xResultado);
        TextView yResultado = (TextView)findViewById(R.id.yResultado);

        EditText txtX= (EditText)findViewById(R.id.txtX);
        EditText txtY= (EditText)findViewById(R.id.txtY);

        //Comeco da Matematica

        float x = Float.parseFloat(txtX.getText().toString());
        float y = Float.parseFloat(txtY.getText().toString());

        float xResult = 5 * x;
        float yResult = 35 * y;

        boolean buttonState = true

        myCanvas.setResults(xResult, yResult);
        myCanvas.setButtonState(buttonState);
    }
}

创建静态变量并在您的 MyCanvas 中使用它class。

在 MainActvity 中:-

    public static float var1 = 1.0f;
    public static float var2 = 2.0f;
    public static boolean var3 = true;

在你的 MyCanvas 中 class:-

    private float var1InCanvas = MainActivity.var1;
    private float var2InCanvas = MainActivity.var2;
    private float var3inCanvas = MainActivity.var3;