EditText不保存输入

时间:2012-06-16 16:17:08

标签: android android-edittext

我是Android的新手,我正在编写一个简单的应用程序来计算射击后留给洞的距离。一切都运行没有错误或警告,但我似乎无法将editText值存储在变量中。我究竟做错了什么?代码中的注释是我尝试过的其他内容。谢谢你的帮助。

package my.pckg.ezgolf;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;

public class EZGolfActivity extends Activity {
    /** Called when the activity is first created. */
    int hole_length;
    int shot_length;
    int dist_remain;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int dist_remain = hole_length - shot_length;

    final EditText edittext = (EditText) findViewById(R.id.editText1);
    edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
              // Perform action on key press
              //Toast.makeText(EZGolfActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                hole_length = Integer.parseInt(edittext.getText().toString());

              return true;
            }
            return false;}               

            TextView myTextView = (TextView) findViewById(R.id.textView4); 
            myTextView.setText("You have this many feet to the pin: " + dist_remain);



    });

    final EditText edittext2 = (EditText) findViewById(R.id.editText2);
    edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
              // Perform action on key press
              //Toast.makeText(EZGolfActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                shot_length = Integer.parseInt(edittext2.getText().toString());
              return true;
            }
            return false;}


            //TextView myTextView = (TextView) findViewById(R.id.textView4); 
            //myTextView.setText("You have this many feet to the pin: " + dist_remain); 

        //int dist_remain = hole_length - shot_length;

        TextView myTextView = (TextView) findViewById(R.id.textView4); 
        myTextView.setText("You have this many feet to the pin: " + dist_remain); 


});

    //int dist_remain = hole_length - shot_length;

    //TextView myTextView = (TextView) findViewById(R.id.textView4); myTextView.setText("You have this many feet to the pin: " + dist_remain);

}

}

4 个答案:

答案 0 :(得分:1)

似乎,您的计算和myTextView的更新仅在Activity的初始化时调用一次。您必须移动计算并调用myTextView.setText到KeyListeners中。 这是未经测试的,但我认为,这将有效。至少应该修复语法错误。如您所见,每次按下回车按钮时都会进行计算,然后才会更新文本视图。

public class EZGolfActivity extends Activity {
    /** Called when the activity is first created. */
    int hole_length;
    int shot_length;
    int dist_remain;

    TextView myTextView = (TextView) findViewById(R.id.textView4);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText edittext = (EditText) findViewById(R.id.editText1);
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    // Toast.makeText(EZGolfActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                    hole_length = Integer.parseInt(edittext.getText().toString());
                    int dist_remain = hole_length - shot_length;
                    myTextView.setText("You have this many feet to the pin: " + dist_remain);
                    return true;
                }
                return false;
            }
        });

        final EditText edittext2 = (EditText) findViewById(R.id.editText2);
        edittext.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    // Toast.makeText(EZGolfActivity.this, edittext.getText(), Toast.LENGTH_SHORT).show();
                    shot_length = Integer.parseInt(edittext2.getText().toString());
                    int dist_remain = hole_length - shot_length;
                    myTextView.setText("You have this many feet to the pin: " + dist_remain);
                    return true;
                }
                return false;
            }
        });
    }
}

答案 1 :(得分:0)

我有一点不大

你已经为edittext

设置了setOnKeyListener

答案 2 :(得分:0)

我认为你不想使用getInteger。你使用parseInt走上了更好的道路:

Why does int num = Integer.getInteger("123") throw NullPointerException?

答案 3 :(得分:0)

您可以尝试使用EditText的addTextChangedListener方法。

 EditText txt_value = (EditText) findViewById(R.id.txt_value);
 txt_value.addTextChangedListener(new TextWatcher() 
    {
        public void  afterTextChanged (Editable s)
        {
            //Log.d("after", "afterTextChanged");
        }
        public void  beforeTextChanged  (CharSequence s, int start, int count, int after)
        {
            //Log.d("before", "beforeTextChanged");
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) 
        {
            txt_value.removeTextChangedListener(this);
            String textValue = s.toString();
            int intValue = Integer.parseInt(textValue);
        }
    });