0

enter image description here

2 buttons, 2 counters for each of them, and switch button.

When switch is ON it needs automatically click on any button(random)

sleep = 100

It'be really great if someone could help with that.

Here is the code of MainActivity:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Button button1;
    private Button button2;

    private TextView text2;
    private Switch switch1;
    private TextView text4;
    private TextView text3;
    private TextView text5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.buttonRed);
        button2 = findViewById(R.id.buttonBlue);

        text2 = findViewById(R.id.textViewBlueScore);
        switch1 = findViewById(R.id.switch1);
        text3 = findViewById(R.id.textViewRedScore);


        final int[] text4 = {0};
        final int[] text5 = {0};

        View.OnClickListener onClickListenerRed = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text5[0] += 1;
                text3.setText(text5[0] + "");
            }
        };
        View.OnClickListener onClickListenerBlue = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text4[0] += 1;
                text2.setText(text4[0] + "");
            }
        };
        button2.setOnClickListener(onClickListenerBlue);
        button1.setOnClickListener(onClickListenerRed);
    }
}




import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Button button1;
    private Button button2;

    private TextView text2;
    private Switch switch1;
    private TextView text4;
    private TextView text3;
    private TextView text5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.buttonRed);
        button2 = findViewById(R.id.buttonBlue);

        text2 = findViewById(R.id.textViewBlueScore);
        switch1 = findViewById(R.id.switch1);
        text3 = findViewById(R.id.textViewRedScore);


        final int[] text4 = {0};
        final int[] text5 = {0};

        View.OnClickListener onClickListenerRed = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text5[0] += 1;
                text3.setText(text5[0] + "");
            }
        };
        View.OnClickListener onClickListenerBlue = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                text4[0] += 1;
                text2.setText(text4[0] + "");
            }
        };
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                int temp = new Random().nextInt()%2;
                View click  =temp == 0 ? button2 : button1;
                if (click != null)
                    click.performClick();
            }
        },100,100);

        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked)
                    timer.cancel();


            }
        });
        button2.setOnClickListener(onClickListenerBlue);
        button1.setOnClickListener(onClickListenerRed);
    }
}

So i don't really get why it crashes fffffffffffffffff

AlexCurie
  • 1
  • 2

1 Answers1

0
Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            int temp = new Random().nextInt()%2;
            View click  =temp == 0 ? button2 : button1;
            if (click != null)
                click.performClick();
        }
    },100,100);
    Switch sw;
    sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (!isChecked)
                timer.cancel();
            else 
                //make timer work
        }
    });

just do it man. Is this something you need?

lantian
  • 176
  • 7
  • An app doesn't even launch.Probably i'm just fool.But i started learning java a few days ago as my first pl. Most likely i shouldn't start with creating app(quickly went over basis before).But i really need it.Please help. I edited my post and added your code.What's incorrect? – AlexCurie May 26 '20 at 10:57
  • show the printstack exception – lantian May 28 '20 at 01:44
  • you need call the view.performClick() on UI thread. use activity.runOnUiThread(new Runnable(){ view.performClick();}) – lantian May 28 '20 at 01:47