下面的代码尝试获取两个 EditText 值,然后将它们转换为整数,然后将它们相除并输出该数字。我没有运气,动不动就遇到一个又一个的路障。我不明白为什么从用户那里获取两个值并将它们分开似乎如此困难。如果有人有答案,请详细解释,因为我是 Java/Android 新手,想了解为什么会发生这种情况。我现在很沮丧,因为我已经为这个单一的问题苦恼了一周。
package com.simplesavinggoal;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
int finalGoal;
EditText goalInput;
EditText monthsNum;
Button enterGoal;
TextView goalOutput;
int goalInputInt;
int monthsNumInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterGoal = (Button) findViewById(R.id.btGetGoal);
goalOutput = (TextView) findViewById(R.id.tvSavingsGoalAmount);
goalInput = (EditText) findViewById(R.id.ndSavingsAmount);
monthsNum = (EditText) findViewById(R.id.ndMonthsNum);
enterGoal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goalInputInt = Integer.parseInt(goalInput.getText().toString());
monthsNumInt = Integer.parseInt(monthsNum.getText().toString());
finalGoal = goalInputInt / monthsNumInt;
goalOutput.setText(finalGoal);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}