我想根据点击事件在 4 个按钮上应用 setAlpha() 函数。下面是我的代码。我准备了一个 4 个数组,其中包含所有 ID。但是 setAlpha() 会抛出错误。有什么建议吗?
错误:找不到符号方法setAlpha(float)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button yellowBtn;
private Button greenBtn;
private Button redBtn;
private Button blueBtn;
private Button startBtn;
String[] tileColor = new String[4];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yellowBtn = findViewById(R.id.yellowBtn);
greenBtn = findViewById(R.id.greenBtn);
blueBtn = findViewById(R.id.blueBtn);
redBtn = findViewById(R.id.redBtn);
startBtn = findViewById(R.id.startBtn);
tileColor[0] = "yellowBtn";
tileColor[1] = "greenBtn";
tileColor[2] = "blueBtn";
tileColor[3] = "redBtn";
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startBtn.setEnabled(false);
randomTile();
}
});
yellowBtn.setOnClickListener(this);
greenBtn.setOnClickListener(this);
blueBtn.setOnClickListener(this);
redBtn.setOnClickListener(this);
}
public void randomTile(){
final int random = new Random().nextInt(4);
AlphaAnimation animationG = new AlphaAnimation(0.2f, 1.0f);
animationG.setDuration(500);
tileColor[random].setAlpha(1f);
tileColor[random].startAnimation(animationG);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.yellowBtn:
AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
animation1.setDuration(500);
yellowBtn.setAlpha(1f);
yellowBtn.startAnimation(animation1);
break;
case R.id.blueBtn:
AlphaAnimation animation2 = new AlphaAnimation(0.2f, 1.0f);
animation2.setDuration(500);
blueBtn.setAlpha(1f);
blueBtn.startAnimation(animation2);
break;
case R.id.greenBtn:
AlphaAnimation animation3 = new AlphaAnimation(0.2f, 1.0f);
animation3.setDuration(500);
greenBtn.setAlpha(1f);
greenBtn.startAnimation(animation3);
break;
case R.id.redBtn:
AlphaAnimation animation4 = new AlphaAnimation(0.2f, 1.0f);
animation4.setDuration(500);
redBtn.setAlpha(1f);
redBtn.startAnimation(animation4);
break;
}
}
}
我想根据点击事件在 4 个按钮上应用 setAlpha() 函数。下面是我的代码。我准备了一个 4 个数组,其中包含所有 ID。但是 setAlpha() 会抛出错误。有什么建议吗?
错误:找不到符号方法setAlpha(float)
Android:错误:找不到符号方法 setAlpha(float)。 setAlpha() 不适用于带有偏移量的数组。
最佳答案
因为您必须在 randomTile
函数中对 View 对象调用 setAlpha(1f)
和 startAnimation(animationG)
。
现在您正在 String 对象上调用这些函数。
关于java - 错误 : cannot find symbol method setAlpha(float). setAlpha() 不适用于具有偏移量的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60241187/