gpt4 book ai didi

java - EditText getText toString 返回 ""

转载 作者:行者123 更新时间:2023-11-30 03:12:26 25 4
gpt4 key购买 nike

我正在开发我的第一个 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”

如有任何帮助,我们将不胜感激。

最佳答案

仅对布局充气一次。

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 是什么。

关于java - EditText getText toString 返回 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20726794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com