简体   繁体   English

EditText getText toString返回“”

[英]EditText getText toString returns “”

I'm working on my first android application, but have some java experience (going to school for comp sci). 我正在开发我的第一个android应用程序,但是有一些Java经验(去上学comp sci)。 I'm working on a 'high scores' system for a small game, and i can't get the name for a score from the EditText object. 我正在为一个小型游戏开发一个“高分”系统,但我无法从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;
    }

}

The string is added to 'scores' which is a static arraylist in MainMenu, which works fine, but when shown scores say " : 100" 字符串被添加到Maincore中的静态数组列表'scores'中,效果很好,但是显示分数时显示“:100”

Any help would be appreciated. 任何帮助,将不胜感激。

Inflate the layout only once. 仅将布局充气一次。

EditText nameInput; // Declare
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_player_game);

Then 然后

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);

Then 然后

 public void updateScores() {
    String name = nameInput.getText().toString();
    MainMenu.scores.add(name + " : " + taps); 
}

I am not sure what MainMenu is. 我不确定什么是MainMenu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM