简体   繁体   English

单击按钮后如何将 integer 值从一个 java 文件传递到其他 java 文件

[英]How to pass integer vales from one java file to other java file after clicking button

I am trying to create multiple rectangles based on user inputs ie col,row.我正在尝试根据用户输入(即 col、row)创建多个矩形。

My requirements are我的要求是

1. I need to pass the two input values(num1,num2) from MainActivity.java between onclick to CanvasActivity.java (col,row). 1.我需要将两个输入值(num1,num2)从MainActivity.java之间的 onclick 传递到CanvasActivity.java (col)。

2.In my code I am able to draw rectangles on creating the app but I need to draw rectangles after the button press. 2.在我的代码中,我可以在创建应用程序时绘制矩形,但我需要在按下按钮后绘制矩形。 ---- this is done ----这就完成了

MainActivity.java MainActivity.java


import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.FrameLayout;


public class MainActivity extends AppCompatActivity {

   

    CanvasActivity drawView;
    Button mButton;
    EditText mEdit1,mEdit2;
    TextView mText,mText1;

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

        mButton = (Button)findViewById(R.id.button);

        mEdit1 = (EditText)findViewById(R.id.editText1);
        mEdit2 = (EditText)findViewById(R.id.editText2);

        mButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                double num1 = Double.parseDouble(mEdit1.getText().toString());
                double num2 = Double.parseDouble(mEdit2.getText().toString());
                double sum = num1 + num2;

                mText = (TextView) findViewById(R.id.textView1);
                mText1 = (TextView) findViewById(R.id.textView3);


                mText.setText("You have entered " + mEdit1.getText().toString() +"x"+ mEdit2.getText().toString()+"Matrix " );
                mText1.setText("Creating " +Double.toString(sum)+" blocks" );
                //setContentView(drawView);


            }
        });

        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.forever);
        frameLayout.addView(new CanvasActivity(this));

    }
}

CanvasActivity.java CanvasActivity.java



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import java.text.BreakIterator;

public class CanvasActivity extends View
{
    // we can directly pass the valuse from this file as bellow
    int left,top=100, width=100,depth=100;
    int i,j,col=3,row=5;



    public CanvasActivity(Context context) { super(context); }

    @Override
    public void onDraw(Canvas canvas)
    {

        Paint paint = new Paint();

        left = 10;
        for (j=0;j<=col;j++)
        {
            for (i=0;i<=row;i++)
            {
                if(i%2 == 0)
                {
                    paint.setColor(Color.RED);
                }
                else
                {
                    paint.setColor(Color.GREEN);
                }
                if ((j%2 == 0) && (i%2 == 1))
                {
                    paint.setColor(Color.BLUE);
                }
                paint.setStyle(Paint.Style.FILL);
                canvas.drawRect((left + (j*width)),(top+(depth*i)),(left + ((j+1)*width)),(top+(depth*(i+1))), paint);
            }
        }
    }
   
}

If you want to pass Width and Height to RectangleView :如果要将WidthHeight传递给RectangleView

RectangleView.java矩形视图.java



import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import java.text.BreakIterator;

public class RectangleViewextends View
{
    // we can directly pass the valuse from this file as bellow
    int left,top=100, width=100,depth=100;
    int i,j,col=3,row=5;



    public RectangleView(Context context, int width, int height) { 
        super(context);
        this.width = width;
        this.height = height;
    }

    @Override
    public void onDraw(Canvas canvas)
    {

        Paint paint = new Paint();

        left = 10;
        for (j=0;j<=col;j++)
        {
            for (i=0;i<=row;i++)
            {
                if(i%2 == 0)
                {
                    paint.setColor(Color.RED);
                }
                else
                {
                    paint.setColor(Color.GREEN);
                }
                if ((j%2 == 0) && (i%2 == 1))
                {
                    paint.setColor(Color.BLUE);
                }
                paint.setStyle(Paint.Style.FILL);
                canvas.drawRect((left + (j*width)),(top+(depth*i)),(left + ((j+1)*width)),(top+(depth*(i+1))), paint);
            }
        }
    }
   
}

and inside MainActivity onClicklistener :MainActivity onClicklistener内部:

RectanlgeView rectangleView = new RectangleView(this, 100, 100);
        frameLayout.addView(rectangleView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将整数值从一个 Java 文件传输到 Android 中的另一个 Java 文件? - How to transfer the integer values from one Java file to other Java file in Android? 如何将字符串从一个Java文件传递到另一个Java文件 - How to Pass String from one java file to another java file 如何获取整数并从文件java中删除其他数据类型? - How to take integer and remove other data types from the file java? 在Java中仅单击一次按钮后如何从多个文本字段打印 - How i can print from more than one textfield after clicking the button just once in java 如何通过单击片段XML文件上的按钮从片段Java文件中打开新的Activity? - How to open new Activity from a fragment java file by clicking the button on the fragment xml file? 单击网站(php)上的按钮后如何弹出Java jar文件? - How to pop up java jar file after clicking button on the website (php)? 如何在Java中从文件正确读取整数 - How to properly read integer from file in Java 如何从Java中的文本文件读取Integer - how to read Integer from a text file in Java 如何在Java中的文件中设置整数或从中获取整数 - how to set and take in a integer to/from a file in java 如何使用Java读取动态增长的文件? 无需点击保存按钮 - How to read a dynamically growing file with Java? without clicking on save button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM