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

huangapple go评论81阅读模式
英文:

How to use a value from one method to other?

问题

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

onCreate中给出值的两个方法:

inheight.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        height = Double.parseDouble(inheight.getText().toString());

        if (KeyEvent.KEYCODE_DEL == keyCode) {
            if (inheight.getText().length() != 0)
                inheight.setText(inheight.getText().subSequence(0, inheight.getText().length() - 1));
        }

        return true;
    }
});



inweight.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        weight = Integer.parseInt(inweight.getText().toString());

        if (KeyEvent.KEYCODE_DEL == keyCode) {
            if (inweight.getText().length() != 0)
                inweight.setText(inweight.getText().subSequence(0, inweight.getText().length() - 1));
        }
        return true;
    }
});

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

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

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

英文:

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:

inheight.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                    height = Double.parseDouble(inheight.getText().toString());

                if(KeyEvent.KEYCODE_DEL== keyCode)
                {
                    if(inheight.getText().length() != 0)
                        inheight.setText(inheight.getText().subSequence(0,inheight.getText().length()-1));
                }

                return true;
            }
        });




        inweight.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                    weight = Integer.parseInt(inweight.getText().toString());

                if(KeyEvent.KEYCODE_DEL == keyCode)
                {
                    if(inweight.getText().length() != 0)
                    inweight.setText(inweight.getText().subSequence(0,inweight.getText().length()-1));
                }
                return true;
            }
        });

And the Calculate method where i try to calulate

    public void calculate()
    {

        if(height!=0 && weight !=0)
       {
// do this
       }
else
{
            Toast.makeText(this, "Error: Values Cannot Be Zero!", Toast.LENGTH_LONG).show();

}

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://go.coder-hub.com/61115531.html
匿名

发表评论

匿名网友

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

确定