简体   繁体   中英

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

I want to transfer 2 float values and 1 boolean value from my MainActivity class to MyCanvas class (which is a class extends View)? Is this possible?

I know this a newbie question, but everything that I found told to use Intent and Bundle , or to use only Bundle.setArguments() , but apparently, none of them work for a View class.

Thank you.

EDIT 1

This is my 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 is like that

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;
    }
}

In this case, I want to transfer:

  • xResult (MainActivity) -> cx (MyCanvas)
  • yResult (MainActivity) -> cy (MyCanvas)
  • buttonState (Main Activity) -> buttonState2 (myCanvas)

You can use setters to set the desired values in your MyCanvas class.

Create methods in your MyCanvas class like this.

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;
    }
}

Then in your activity class

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);
    }
}

Create static variable and use it in your MyCanvas class.

In MainActvity:-

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

And in your MyCanvas class:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM