我是 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);
}
}