【发布时间】:2013-12-22 06:15:58
【问题描述】:
我正在开发我的第一个 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中的一个静态数组列表,它工作正常,但是当显示分数时显示“:100”
任何帮助将不胜感激。
【问题讨论】:
-
你的
.inflate()函数是什么? -
点击按钮
String name = nameInput.getText().toString()。您已经初始化了editext,除非您将文本设置为xml中的edittext,否则您将无法获得名称。 -
这是从按钮的onClick方法中调用的
-
@Phrank 发布整个代码。
layout设置为活动? -
@Phrank
View layout = inflater.inflate(R.layout.popup_element,(ViewGroup) findViewById(R.id.popup_element));和这个View layout = View.inflate(this, R.layout.popup_element, null);膨胀两次