我正在开发我的第一个Android应用程序,但有一些java经验(去comp sci上学)。我正在为一个小游戏开发一个“高分”系统,我无法从EditText对象获得一个得分的名称。
public class SinglePlayerGame extends Activity {
private Button mashButton, popButton;
int taps;
private TextView numberOutput, clock, popView;
boolean first;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_player_game);
taps = 0;
first = true;
numberOutput = (TextView) findViewById(R.id.editText1);
mashButton = (Button) findViewById(R.id.mash_button1);
clock = (TextView) findViewById(R.id.clockView);
mashButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(first) {
runClock();
addOne();
first = false;
} else {
addOne();
}
}
});
}
protected void runClock() {
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
clock.setText(DateUtils.formatElapsedTime(millisUntilFinished / 1000));
}
public void onFinish() {
mashButton.setEnabled(false);
clock.setText("Done!");
initPopWin();
}
}.start();
}
public void updateScores() {
View layout = View.inflate(this, R.layout.popup_element, null);
EditText nameInput = (EditText) layout.findViewById(R.id.popupName);
String name = nameInput.getText().toString();
MainMenu.scores.add(name + " : " + taps);
}
private PopupWindow pwindo;
protected void initPopWin() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) SinglePlayerGame.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_element,(ViewGroup) findViewById(R.id.popup_element));
popButton = (Button) layout.findViewById(R.id.btn_close_popup);
popButton.setOnClickListener(cancel_button_click_listener);
popView = (TextView) layout.findViewById(R.id.txtView);
popView.setText("Congrats your score was: " + taps + "!");
pwindo = new PopupWindow(layout, 350, 350, true);
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
private OnClickListener cancel_button_click_listener = new OnClickListener() {
public void onClick(View v) {
updateScores();
pwindo.dismiss();
}
};
protected void addOne() {
// TODO Auto-generated method stub
taps += 1;
numberOutput.setText(Integer.toString(taps));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.single_player_game, menu);
return true;
}
}
该字符串被添加到'scores'中,这是MainMenu中的静态arraylist,它工作正常,但是当显示分数时说“:100”
任何帮助都将不胜感激。
答案 0 :(得分:1)
仅对布局进行一次充气。
EditText nameInput; // Declare
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_player_game);
然后
protected void initPopWin() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) SinglePlayerGame.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_element,null);
nameInput = (EditText) layout.findViewById(R.id.popupName);
然后
public void updateScores() {
String name = nameInput.getText().toString();
MainMenu.scores.add(name + " : " + taps);
}
我不确定MainMenu
是什么。