D
D
Danil Tolkachev2019-05-10 12:45:45
Java
Danil Tolkachev, 2019-05-10 12:45:45

How to transfer the value of a variable to another screen?

Hello! Could you help me? I am writing a simple program ala hit the mug. And you need something when you hit him, plus points. That's what I did, but how do I get my results variable (the one for scoring) to go to the next screen? I did this with the putExtra method, but for some reason txt(TextView set to display the result) does not display anything at all. Below are two codes: for the first and second screen, respectively

public class Challenge1 extends Activity {
    DrawView mDW;

    Bitmap mbitmap;
    Canvas mcanvas;
    Paint mPaint;
    float dx = 1, dy = 1;
    float x = 0, y = 0;
    Random rnd = new Random();
    private Timer mTimer;
    private MyTimerTask mMyTimerTask;
    int results=0;
    int seconds=0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDW = new DrawView(this);

        setContentView(mDW);
        if (mTimer != null) {
            mTimer.cancel();

        }
        mTimer = new Timer();
        mMyTimerTask = new MyTimerTask();
        mTimer.schedule(mMyTimerTask, 0, 1000);
    }

    class DrawView extends View {


        public DrawView(Context context) {
            super(context);
            mPaint = new Paint();
            mbitmap = Bitmap.createBitmap(700, 1000, Bitmap.Config.ARGB_8888);
            mbitmap.setDensity(DisplayMetrics.DENSITY_MEDIUM);
            mcanvas = new Canvas(mbitmap);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.FILL);
            mcanvas.drawColor(Color.GRAY);//нарисуй фон


        }

        protected void onDraw(Canvas canvas) {
            float canvasy = canvas.getHeight();
            float canvasx = canvas.getWidth();
            dx = canvasx / 700f;
            dy = canvasy / 1000f;
            canvas.scale(dx, dy);
            //здесь код перерисовки
            x = rnd.nextInt(650);
            y =rnd.nextInt(950);


            mcanvas.drawColor(Color.GRAY);//нарисуй фон
            mcanvas.drawCircle(x, y, 50, mPaint);//нарисуй окружность в случайном месте
            canvas.drawBitmap(mbitmap, 0, 0, mPaint);
        }

        public boolean onTouchEvent(MotionEvent event)
        {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                float Width= event.getX()/dx;
                float Height = event.getY()/dy;
              
                if(Width>x-50 && Width<x+50 && Height>y-50 && Height<y+50)
                {
                    results++;// если попадаешь по кружку плюсуются очки

                }
                else
                {
                    results--;
                    if(results<0)// иначе очки отнимаются. Если число очков меньше 0, то они обнуляются просто
                    {
                        results=0;
                    }
               }

                invalidate();

            }
         return true;
        }
    }

    //класс myTinerTask
    class MyTimerTask extends TimerTask {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mDW.invalidate();//обнови картинку
                    seconds++;
                    if(seconds>5){
                        mTimer.cancel();
                        mTimer = null;
                        Intent mintent = new Intent(Challenge1.this, Results.class);
                         mintent.putExtra("res",results);
                        startActivity(mintent);// после 5 секунд переход на след экран


                    }


                }
            });


        }
    }


}

public class Results extends Activity {

    TextView txt;

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

        txt=(TextView)findViewById(R.id.txt) ;

        Intent mintent=getIntent();
        String resultat=mintent.getExtras().getString("res");
        txt.setText("Ваш результат "+resultat);








    }

}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question