gpt4 book ai didi

java - Java 上的自动点击器(Android Studio)

转载 作者:行者123 更新时间:2023-12-01 16:32:14 26 4
gpt4 key购买 nike

enter image description here

2 个按钮,每个按钮 2 个计数器,以及切换按钮。

当开关打开时,需要自动点击任意按钮(随机)

sleep =100

如果有人能提供帮助,那就太好了。

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

所以我真的不明白为什么它崩溃ffffffffffffffff

最佳答案

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

就做吧,伙计。这是您需要的吗?

关于java - Java 上的自动点击器(Android Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62018946/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com