android工作室:更改runOnUiThread中按钮的特性,阻止这些按钮



我有两个按钮:卖出和买入。

1.当我点击销售时,我会在销售按钮上调用setOnClickistener。在内部,我正在创建一个线程(出于特定原因,我需要它(,在线程内部,我使用以下代码:

runOnUiThread(new Runnable() {
@Override
public void run() {
Button buyButton = findViewById(R.id.buy_button);
buyButton.setEnabled(true);
Button sellButton = findViewById(R.id.sell_button);
sellButton.setBackgroundDrawable(buyButton.getBackground());
sellButton.setText(getResources().getString(R.string.stop_selling));
sellButton.setTextColor(getResources().getColor(android.R.color.white));
}
});

2.我再次点击销售按钮,以便恢复正常。因此,我再次在销售按钮上设置OnClickistener。在内部我再次创建一个线程,在线程内部我使用以下代码:

runOnUiThread(new Runnable() {
@Override
public void run() {
Button buyButton = findViewById(R.id.buy_button);
buyButton.setEnabled(true);
Button sellButton = findViewById(R.id.sell_button);
sellButton.setBackgroundDrawable(buyButton.getBackground());
sellButton.setText(getResources().getString (R.string.sell));
sellButton.setTextColor(resTextColorToChange);
}
});

然后我点击购买按钮。我在购买按钮上调用setOnClickistener。在内部,我正在创建一个线程(出于特定原因,我需要它(,在线程内部,我使用以下代码:

runOnUiThread(new Runnable() {
@Override
public void run() {
Button sellButton =  findViewById(R.id.sell_button);
Button buyButton =  findViewById(R.id.buy_button);
sellButton.setEnabled(false);
buyButton.setEnabled(false);
}
});

我预计买卖按钮将不启用。。。按钮购买未启用。。。但按钮sell仍处于启用状态!你知道为什么吗??

请参阅runOnUiThread的文档,您会看到如果当前线程不是UI线程,则不会立即执行该操作。相反,它被发布到事件队列中。

现在,你的情况就是这样。因此,您的Runnable中的任何一个都可以不按特定顺序执行。

您可以做的是记录Runnable的执行,因为您有很多,并确保没有不需要的执行被执行,并且没有按不需要的顺序执行。

为了更准确地回答这个问题,我测试了另一个类似的代码,它是:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
int a = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.sell_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(a==0) {
findViewById(R.id.buy_button).setEnabled(false);
Button sellButton = findViewById(R.id.sell_button);
sellButton.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
sellButton.setText("STOP SELLING");
sellButton.setTextColor(getResources().getColor(android.R.color.white));
a=1;
}
else{
Button buyButton = findViewById(R.id.buy_button);
buyButton.setEnabled(true);
Button sellButton = findViewById(R.id.sell_button);
sellButton.setBackgroundDrawable(buyButton.getBackground());
sellButton.setText("SELL DATA");
sellButton.setTextColor(buyButton.getTextColors().getDefaultColor());
a=0;
}
}
});
findViewById(R.id.buy_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Button sellButton =  findViewById(R.id.sell_button);
Button buyButton =  findViewById(R.id.buy_button);
sellButton.setEnabled(false);
buyButton.setEnabled(false);
}
});
}

}

这里没有runOnThreadUi((,但如果我再次单击"卖出",然后再次单击"卖",然后单击"买":按钮"买"没有启用,但按钮"卖"仍然启用,这听起来不合理!

最新更新