相当于一套=和getActionCommand Android中Java的? [英] Equivalent of set= and getActionCommand in Android Java?
问题描述
我刚刚开始学习Android开发,并作为一个小项目,我建立一个计算器。它的工作方式是,当一个数字按钮pssed $ P $,数量追加到的EditText。我不想写这code每个按钮的,因为它的繁琐和效率低下的招摇,但我不知道如何去做。
I've just started learning Android development and as a little project, I'm building a calculator. The way it works is that when a number button is pressed, the number is appended to the EditText. I don't want to write this code for each of the buttons, because it's tedious and blatantly inefficient but i'm not sure how to go about it.
当我用Java编程,我解决此问题得到了通过设置一个JButton的ActionCommand等于号码,然后进行的是一般
When I programmed in Java, I got around the problem by setting the ActionCommand of the JButton equal to the number and then making a general
textField.append(button.getActionCommand());
这是可能的Android中?有没有更好的方法吗?
感谢您的帮助!
Is this possible in Android? Is there a better approach? Thanks for your help!
推荐答案
您可以添加标签到每个按钮。在的onClick
方法检索按钮标签并添加它来编辑框。 OnClickListener
所有数字按钮将是相同的:
You can add tag to every button. In onClick
method retrieve tag from button and append it to edit box. OnClickListener
for all number buttons will be the same:
public void onClick(View v) {
String value = v.getTag();
editText.getText().append(value);
}
所以,你可以使用 OnClickListener
1实例的所有按钮。
这篇关于相当于一套=和getActionCommand Android中Java的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!