Android: 按钮方法只工作一次
Android: Button method works only once
我是 Android 的 Java 的新手(所以请不要打我),我对我的按钮操作有疑问。被调用的方法只有一次运行。当我第二次点击按钮时,什么也没有发生。我不懂为什么。没有错误,没有缺陷,该方法正在执行预期的操作。有什么提示吗?
谢谢!
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView mainFortuneTextView;
Button mainFortuneButton;
private int counter, i, x;
//private int randomNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// 1. Access the TextView defined in layout XML
// and then set its text
mainFortuneTextView = (TextView) findViewById(R.id.fortuneTextView);
// 2. Access the Button defined in layout XML
// and listen for it here by using "this"
mainFortuneButton = (Button) findViewById(R.id.fortuneButton);
mainFortuneButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// happens on button action in main view
runThread();
Random rnd = new Random();
x = rnd.nextInt(11) + 1;
}
private void runThread() {
mainFortuneButton.setEnabled(false);
new Thread() {
public void run() {
while (i++ < 10) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
mainFortuneTextView.setText("#" + i);
}
});
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// activate button again
runOnUiThread(new Runnable() {
@Override
public void run() {
mainFortuneButton.setEnabled(true);
}
});
}
}.start();
}
}
您在 while 中使用的变量 i
是一个字段。这意味着它不会重置。这就是为什么您第二次调用线程时 i
值将为 10 并且不会再次调用的原因。在开始新线程之前,您必须再次重置 i
。
我是 Android 的 Java 的新手(所以请不要打我),我对我的按钮操作有疑问。被调用的方法只有一次运行。当我第二次点击按钮时,什么也没有发生。我不懂为什么。没有错误,没有缺陷,该方法正在执行预期的操作。有什么提示吗? 谢谢!
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView mainFortuneTextView;
Button mainFortuneButton;
private int counter, i, x;
//private int randomNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// 1. Access the TextView defined in layout XML
// and then set its text
mainFortuneTextView = (TextView) findViewById(R.id.fortuneTextView);
// 2. Access the Button defined in layout XML
// and listen for it here by using "this"
mainFortuneButton = (Button) findViewById(R.id.fortuneButton);
mainFortuneButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// happens on button action in main view
runThread();
Random rnd = new Random();
x = rnd.nextInt(11) + 1;
}
private void runThread() {
mainFortuneButton.setEnabled(false);
new Thread() {
public void run() {
while (i++ < 10) {
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
mainFortuneTextView.setText("#" + i);
}
});
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// activate button again
runOnUiThread(new Runnable() {
@Override
public void run() {
mainFortuneButton.setEnabled(true);
}
});
}
}.start();
}
}
您在 while 中使用的变量 i
是一个字段。这意味着它不会重置。这就是为什么您第二次调用线程时 i
值将为 10 并且不会再次调用的原因。在开始新线程之前,您必须再次重置 i
。