0

我是 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

4 回答 4

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;
            }
        });
    }
}
于 2012-06-16T16:23:49.573 回答
0

我有一点不大

你已经为 edittext 设置了 setOnKeyListener

于 2012-06-16T16:22:06.583 回答
0

我认为您不想使用 getInteger。使用 parseInt,您走上了一条更好的道路:

为什么 int num = Integer.getInteger("123") 会抛出 NullPointerException?

于 2012-06-16T16:26:43.430 回答
0

您可以尝试使用addTextChangedListenerEditText 的方法。

 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);
        }
    });
于 2012-06-18T14:20:21.010 回答