This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(20个答案)
25天前关闭。
enter image description here
2个按钮,每个都有2个计数器,还有切换按钮。
当开关打开时,它需要自动单击任何按钮(随机)
睡眠= 100
如果有人可以提供帮助,那真是太好了。
这是MainActivity的代码:
所以我真的不明白为什么它崩溃
fffffffffffffffffff
做男人。
这是您需要的东西吗?
(20个答案)
25天前关闭。
enter image description here
2个按钮,每个都有2个计数器,还有切换按钮。
当开关打开时,它需要自动单击任何按钮(随机)
睡眠= 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);
}
}
所以我真的不明白为什么它崩溃
fffffffffffffffffff
最佳答案
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/