我正在开发一个包含几个按钮的应用程序。
我需要您的帮助,看看我是否可以改进代码。
这些动作可以做同样的事情,只是改变背景色并使按钮的值传递给我。
该应用程序还有其他几个屏幕,这些屏幕上的许多按钮具有几乎相同的操作,只是返回值有所变化。
(Image) I'm using linear layout
我的代码:
btnBall01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count1++;
selectedNumber++;
games.setBallId(btnBall01.getId());
btnBall01.setBackgroundResource(R.mipmap.ball_verde);
games.setActualNumber(btnBall01.getText().toString());
Toast.makeText(ActivityMegasenaCreateGame.this, "Verde " + games.getActualNumber(), Toast.LENGTH_SHORT).show();
if (count1 != 1){
count1 = 0;
selectedNumber = selectedNumber -2;
btnBall01.setBackgroundResource(R.mipmap.ball);
Toast.makeText(ActivityMegasenaCreateGame.this, "Branco " + games.getActualNumber(), Toast.LENGTH_SHORT).show();
initializeComponentsBallsButtons();
}
}
});
btnBall02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnBall02.setBackgroundResource(R.mipmap.ball_verde);
count2++;
selectedNumber++;
games.setBallId(btnBall02.getId());
games.setActualNumber(btnBall02.getText().toString());
if (count2 != 1){
count2 = 0;
selectedNumber = selectedNumber -2;
btnBall02.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall03.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall03.setBackgroundResource(R.mipmap.ball_verde);
count3++;
selectedNumber++;
games.setBallId(btnBall03.getId());
games.setActualNumber(btnBall03.getText().toString());
if (count3 !=1){
count3 = 0;
selectedNumber = selectedNumber -2;
btnBall03.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall04.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall04.setBackgroundResource(R.mipmap.ball_verde);
count4++;
selectedNumber++;
games.setBallId(btnBall04.getId());
games.setActualNumber(btnBall04.getText().toString());
if (count4 != 1){
count4 = 0;
selectedNumber = selectedNumber -2;
btnBall04.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall05.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall05.setBackgroundResource(R.mipmap.ball_verde);
count5++;
selectedNumber++;
games.setBallId(btnBall05.getId());
games.setActualNumber(btnBall05.getText().toString());
if (count5 != 1){
count5 = 0;
selectedNumber = selectedNumber -2;
btnBall05.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall06.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall06.setBackgroundResource(R.mipmap.ball_verde);
count6++;
selectedNumber++;
games.setBallId(btnBall06.getId());
games.setActualNumber(btnBall06.getText().toString());
if (count6 != 1){
count6 = 0;
selectedNumber = selectedNumber -2;
btnBall06.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall07.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall07.setBackgroundResource(R.mipmap.ball_verde);
count7++;
selectedNumber++;
games.setBallId(btnBall07.getId());
games.setActualNumber(btnBall07.getText().toString());
if (count7 != 1){
count7 = 0;
selectedNumber = selectedNumber -2;
btnBall07.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall08.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall08.setBackgroundResource(R.mipmap.ball_verde);
count8++;
selectedNumber++;
games.setBallId(btnBall08.getId());
games.setActualNumber(btnBall08.getText().toString());
if (count8 != 1){
count8 = 0;
selectedNumber = selectedNumber -2;
btnBall08.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall09.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall09.setBackgroundResource(R.mipmap.ball_verde);
count9++;
selectedNumber++;
games.setBallId(btnBall09.getId());
games.setActualNumber(btnBall09.getText().toString());
if (count9 != 1){
count9 = 0;
selectedNumber = selectedNumber -2;
btnBall09.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall10.setBackgroundResource(R.mipmap.ball_verde);
count10++;
selectedNumber++;
games.setBallId(btnBall10.getId());
games.setActualNumber(btnBall10.getText().toString());
if (count10 != 1){
count10 = 0;
selectedNumber = selectedNumber -2;
btnBall10.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall11.setBackgroundResource(R.mipmap.ball_verde);
count11++;
selectedNumber++;
games.setBallId(btnBall11.getId());
games.setActualNumber(btnBall11.getText().toString());
if (count11 != 1){
count11 = 0;
selectedNumber = selectedNumber -2;
btnBall11.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall12.setBackgroundResource(R.mipmap.ball_verde);
count12++;
selectedNumber++;
games.setBallId(btnBall12.getId());
games.setActualNumber(btnBall12.getText().toString());
if(count12 != 1){
count12 = 0;
selectedNumber = selectedNumber -2;
btnBall12.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall13.setBackgroundResource(R.mipmap.ball_verde);
count13++;
selectedNumber++;
games.setBallId(btnBall13.getId());
games.setActualNumber(btnBall13.getText().toString());
if(count13 != 1){
count13 = 0;
selectedNumber = selectedNumber -2;
btnBall13.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall14.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall14.setBackgroundResource(R.mipmap.ball_verde);
count14++;
selectedNumber++;
games.setBallId(btnBall14.getId());
games.setActualNumber(btnBall14.getText().toString());
if (count14 != 1){
count14 = 0;
selectedNumber = selectedNumber -2;
btnBall14.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall15.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall15.setBackgroundResource(R.mipmap.ball_verde);
count15++;
selectedNumber++;
games.setBallId(btnBall15.getId());
games.setActualNumber(btnBall15.getText().toString());
if (count15 != 1){
count15 = 0;
selectedNumber = selectedNumber -2;
btnBall15.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
答案 0 :(得分:0)
您可以尝试以这种方式实现
//Generate buttons
for (int i = 0; i <= buttonCount; ++i) {
Button btn = new Button(context);
btn.setText(String.valueOf(i));
btn.setOnClickListener(buttonClickListener);
linearLayout.addView(btn);
}
//OnClickListener
OnClickListener buttonClickListener = new OnClickListener() {
@Override
public void onClick(final View view) {
Button btn = (Button) view;
String str = btn.getText().toString();
int index = Integer.parseInt(str); /*<-- create a method using this index*/
}
};
现在,您知道单击了哪个按钮。根据您的按钮索引进行计算。
NB:这不是您需要的完整代码,只是实现的基本逻辑。