所以我正在开发Android程序,显示我的年龄给出了他们的出生年份。但我的应用程序不显示任何答案,它只显示0按钮时按下。 您能否请我建议我去哪里错了。并告诉我如何显示2017年之间的差异 - myAge。如何让我的显示在我的textView在Android?
public class MainActivity extends AppCompatActivity {
Button press;
EditText editText;
TextView enterText;
int myAge;
int yearOfBirth;
int year = 2017;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText) ;
enterText = (TextView)findViewById(R.id.entertext);
press = (Button)findViewById(R.id.press);
try
{
yearOfBirth = Integer.parseInt(editText.getText().toString());
}catch(NumberFormatException e)
{
e.printStackTrace();
}
press.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAge = yearOfBirth;
enterText.setText(String.valueOf(myAge));
}
});
}
}
截图:
您应该从您的点击侦听器中获取edittext的值。目前,您可以在应用程序启动时获取编辑文本的值(onCreate)。如果你想获得编辑文本的值,当你按下一个按钮,你需要在onClickListener – swerly
并获得正确的输出从'yearOfBirth'减去'年' – Blasanka