私はアンドロイドが初めてで、ショット後に穴までの距離を計算する簡単なアプリを書いています。すべてがエラーや警告なしで実行されますが、変数に格納される 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);
}
}