我想基于单击事件在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()不适用于具有偏移量的Array。
最佳答案
因为您必须在setAlpha(1f)
函数中的视图对象上调用startAnimation(animationG)
和randomTile
。
现在您要在String对象上调用这些函数。