如何将一个方法中的值用于另一个方法?

huangapple 未分类评论50阅读模式
英文:

How to use a value from one method to other?

问题

于是我在Android Studio中正在开发一个bmi计算器活动。然后我遇到了一个问题,我从类中为两个不同的TextView获取了两个方法内的变量值。这本不应该成为问题,但一旦我在应用程序中切换到另一个TextView,第一个TextView就会发送一个零值,这就是问题所在。有人知道如何修复吗?谢谢!

onCreate中给出值的两个方法:

  1. inheight.setOnKeyListener(new View.OnKeyListener() {
  2. @Override
  3. public boolean onKey(View v, int keyCode, KeyEvent event) {
  4. height = Double.parseDouble(inheight.getText().toString());
  5. if (KeyEvent.KEYCODE_DEL == keyCode) {
  6. if (inheight.getText().length() != 0)
  7. inheight.setText(inheight.getText().subSequence(0, inheight.getText().length() - 1));
  8. }
  9. return true;
  10. }
  11. });
  12. inweight.setOnKeyListener(new View.OnKeyListener() {
  13. @Override
  14. public boolean onKey(View v, int keyCode, KeyEvent event) {
  15. weight = Integer.parseInt(inweight.getText().toString());
  16. if (KeyEvent.KEYCODE_DEL == keyCode) {
  17. if (inweight.getText().length() != 0)
  18. inweight.setText(inweight.getText().subSequence(0, inweight.getText().length() - 1));
  19. }
  20. return true;
  21. }
  22. });

以及我尝试计算的Calculate方法:

  1. public void calculate() {
  2. if (height != 0 && weight != 0) {
  3. // 进行计算
  4. } else {
  5. Toast.makeText(this, "错误:值不能为零!", Toast.LENGTH_LONG).show();
  6. }
  7. }

因此,当我运行它时,总是显示弹出消息 :(。请帮忙!

英文:

So i was developing a bmi calculator activity in android studio. And i ran into a problem where i got a value of two variable inside from two methods for different TextView which was defined in the class. That shouldn't be problem but once i switch to another TextView in app. The first one sends a zero value, which is the problem, Anyone knows how to fix it? Thanks !

The two methods in onCreate which give value:

  1. inheight.setOnKeyListener(new View.OnKeyListener() {
  2. @Override
  3. public boolean onKey(View v, int keyCode, KeyEvent event) {
  4. height = Double.parseDouble(inheight.getText().toString());
  5. if(KeyEvent.KEYCODE_DEL== keyCode)
  6. {
  7. if(inheight.getText().length() != 0)
  8. inheight.setText(inheight.getText().subSequence(0,inheight.getText().length()-1));
  9. }
  10. return true;
  11. }
  12. });
  13. inweight.setOnKeyListener(new View.OnKeyListener() {
  14. @Override
  15. public boolean onKey(View v, int keyCode, KeyEvent event) {
  16. weight = Integer.parseInt(inweight.getText().toString());
  17. if(KeyEvent.KEYCODE_DEL == keyCode)
  18. {
  19. if(inweight.getText().length() != 0)
  20. inweight.setText(inweight.getText().subSequence(0,inweight.getText().length()-1));
  21. }
  22. return true;
  23. }
  24. });

And the Calculate method where i try to calulate

  1. public void calculate()
  2. {
  3. if(height!=0 && weight !=0)
  4. {
  5. // do this
  6. }
  7. else
  8. {
  9. Toast.makeText(this, "Error: Values Cannot Be Zero!", Toast.LENGTH_LONG).show();
  10. }

So when i run it always shows the toast message :(. Please Help!

答案1

得分: 0

安卓文档中所述,View.OnKeyListener仅对硬件键盘有效,而软件输入方法(例如安卓使用的方法)无需触发此监听器的义务。

因此,与其使用View.OnKeyListener来获取您的值,我建议尝试不同的方法(例如,在按下按钮后获取值,实现View.OnClickListener)。

另外,您确定仅第一个TextView给您零值,还是两个TextView都是?

英文:

As stated in the android documentation, View.OnKeyListener is useful only for hardware keyboards , and a software input method (like the one android uses), has no obligation to trigger this listener.

So instead of using View.OnKeyListener to get your values i would try a different approach (i.e getting the values after pressing a button, implementing View.OnClickListener )

Also are you sure that only the first TextView gives you the zero value, or both ?

huangapple
  • 本文由 发表于 2020年4月9日 14:56:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/61115531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定