我正在研究我的文字冒险游戏,并想知道是否有一种更简单的方法来编写重复的代码块,例如我在下面的内容。
在此块中,向用户显示北,东,南和西的N,E,S,W。所以我分别编写每个监听器,并在每个监听器中包含一个try / catch块。但现在整个代码块似乎都是重复的。
以下是代码块:
btnNorth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tvIntro.setText("You go north");
String testString = "nothing";
try {
testString = Encounter.EncounterGeneratorText();
} catch (Exception e) {
e.printStackTrace();
}
testString = Encounter.EncounterGeneratorText();
tvIntro.setText(testString);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tvIntro.setText(testString);
}
});
btnEast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tvIntro.setText("You go east");
String testString = "nothing";
try {
testString = Encounter.EncounterGeneratorText();
} catch (Exception e) {
e.printStackTrace();
}
testString = Encounter.EncounterGeneratorText();
tvIntro.setText(testString);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tvIntro.setText(testString);
}
});
btnSouth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tvIntro.setText("You go south");
String testString = "nothing";
try {
testString = Encounter.EncounterGeneratorText();
} catch (Exception e) {
e.printStackTrace();
}
testString = Encounter.EncounterGeneratorText();
tvIntro.setText(testString);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tvIntro.setText(testString);
}
});
btnWest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tvIntro.setText("you go west");
String testString = "nothing";
try {
testString = Encounter.EncounterGeneratorText();
} catch (Exception e) {
e.printStackTrace();
}
testString = Encounter.EncounterGeneratorText();
tvIntro.setText(testString);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tvIntro.setText(testString);
};
});
答案 0 :(得分:10)
您可以创建一个所有按钮都将使用的OnClickListener,在OnClickListener中,您将检测单击了哪个按钮并在switch语句中执行特定于方向的任务。
这样的事情:
private OnClickListener DirectionClickListner = new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.north:
//Set your strings for North
break;
case R.id.west:
//Set your strings for West
break;
case R.id.east:
//Set your strings for East
break;
case R.id.south:
//Set your strings for South
break;
}
}
};
答案 1 :(得分:7)
为什么不把它抽象成单个函数,比如这个(伪代码):
btnNorth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
move("North");
}
});
btnSouth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
move("South");
}
});
btnEast.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
move("East");
}
});
btnWest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
move("West");
}
});
void move(String direction) {
tvIntro.setText("You go " + direction);
String testString = "nothing";
try {
testString = Encounter.EncounterGeneratorText();
} catch (Exception e) {
e.printStackTrace();
}
testString = Encounter.EncounterGeneratorText();
tvIntro.setText(testString);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tvIntro.setText(testString);
}
你当然可以更多地清理它,但至少大部分常见代码都在一个位置:)
答案 2 :(得分:2)
是。您应该保存与成员变量执行相同操作的侦听器。
private OnClickListener mOnClickListener = new OnClickListener() { ... };
并使用这样的。
btnNorth.setOnClickListener(mOnClickListener);
btnEast.setOnClickListener(mOnClickListener);
btnSouth.setOnClickListener(mOnClickListener);
答案 3 :(得分:2)
您可以尝试将游戏逻辑封装在自己的类中。所以每个OnClickListener
只有一个这个类的实例,然后他们就可以在那个类上调用goEast()
或goWest()
等。这将把所有逻辑保留在一个类中,并减少重复代码的数量。
答案 4 :(得分:2)
我会尝试以测试为导向的开发。如果你总是做最简单的事情,你会发现重构代码更容易,这样你就可以避免重复自己。
答案 5 :(得分:2)
使用OO方法修改了您的示例,这使您的代码也更易于测试。
static interface DirectionProvider {
String getDirection();
}
DirectionProvider north = new DirectionProvider() {
public String getDirection() {
return "You go north";
}
};
DirectionProvider south = new DirectionProvider() {
public String getDirection() {
return "You go south";
}
};
DirectionProvider east = new DirectionProvider() {
public String getDirection() {
return "You go east";
}
};
DirectionProvider west = new DirectionProvider() {
public String getDirection() {
return "You go west";
}
};
static class MyOnClickListener extends OnClickListener {
public MyOnClickListener(DirectionProvider dp) {
@Override
public void onClick(View v) {
tvIntro.setText(dp.getDirection());
String testString = "nothing";
try {
testString = Encounter.EncounterGeneratorText();
} catch (Exception e) {
e.printStackTrace();
}
testString = Encounter.EncounterGeneratorText();
tvIntro.setText(testString);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tvIntro.setText(testString);
}
}
}
你可以像这样使用你的新听众:
btnEast.setOnClickListener(new MyOnClickListener(north));
btnSouth.setOnClickListener(new MyOnClickListener(south));
btnWest.setOnClickListener(new MyOnClickListener(west));
btnEast.setOnClickListener(new MyOnClickListener(east));