Android计算器应用前端:如何使用按钮而非键盘允许数字输入?

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

Android calculator app frontend: How to allow number inputs using buttons instead of keyboard?

问题

我们都知道计算器是如何工作的。我正在尝试在Android Studio中制作一个计算器应用。问题是,我不希望用户使用键盘输入数字,而是希望用户在应用程序上使用按钮。如下图所示-

现在的问题是如何实现这个按钮功能?当用户点击按钮1时,编辑文本应该显示1,如果按下2,然后在编辑文本中显示1后面跟着2。我完全没有任何想法。我只想在编辑文本中像这样显示一个方程式456+54-56/5。我可能可以编写数学计算的代码,但我无法获得数字。我也不知道如何在编辑文本中接收输入,除了键盘,而键盘不是真正的计算器方式。

我还没有尝试过任何方法。我有点像这样做-

Button C_button = findViewById(R.id.c);
C_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        ((EditText)findViewById(R.id.et_ans)).setText("0");
    }
});

但我知道为什么它不起作用,它只是在每次点击零按钮时将字符串"0"一起放入编辑文本中。但不像键盘输入那样工作。有人可以帮我解决这个问题吗?

英文:

We all know how calculator works. I am trying to make a calculator in android studio. The thing is I don't want the user to use their keyboard to give number inputs, instead the user is gonna use the buttons on the app. The picture shown below-
Android计算器应用前端:如何使用按钮而非键盘允许数字输入?

Now the question is how to do this button thing? When user clicks button 1 the edittext should show 1, if pressed 2 then show 2 after the 1 in edittext. I am not having any idea at all. I just want an equation in the edittext like 456+54-56/5 like this. I can probably do the math coding but I can't have the numbers. And I don't know how to take input in the edittext other than the keyboard which is not really a calculator thing.

I couldn't try anything. I kinda did this-

Button C_button = findViewById(R.id.c);
        C_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((EditText)findViewById(R.id.et_ans)).setText("0");
            }
        });

But I know why it doesn't work, it just simply put the string zero all together in the edittext every time I click the zero button. But not working like the keyboard entry. Can somebody help me with this?

答案1

得分: 2

你可以将值存储在一个字符串变量中,当你按下一个新键时,只需将该值附加到你的字符串变量中,并将其设置为你的editText。

假设你创建一个实例变量,如下所示:

String myValue = "";

现在你按下数字1的按钮,点击按钮后的逻辑是:

@Override
public void onClick(View view) {
    myValue = myValue + "1";
    ((EditText)findViewById(R.id.et_ans)).setText(myValue);
}

希望这对你有帮助。
你也可以使用StringBuilder来实现相同的功能。

英文:

You can store value in a string variable and when you press a new key you just append that value in your string variable and set this to your editText.
suppose you create a instance variable like -

String myValue = "";

and now you press the button having number 1, the logic after you click the button is -

@Override
public void onClick(View view) {
    myValue = myValue+"1";
    ((EditText)findViewById(R.id.et_ans)).setText(myValue);
}

Hope this will helpful to you.
You can also use StringBuilder for the same.

答案2

得分: 1

你可以获取EditText中已存在的字符串,然后将新的数字连接起来。

示例:

EditText edt = ((EditText)findViewById(R.id.et_ans));
Button one_button = findViewById(R.id.one);
one_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        String a = edt.getText().toString();
        edt.setText(a + "1");
    }
});
英文:

You can fetch the string that is already present in the EditText and then concatenate the new number present.

Ex: -

EditText edt = ((EditText)findViewById(R.id.et_ans));
Button one_button = findViewById(R.id.one);
     one_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                  String a=edt.getText().toString();
                  edt.setText(a+ "1");
                }
            });

huangapple
  • 本文由 发表于 2023年5月22日 18:03:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305043.html
匿名

发表评论

匿名网友

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

确定