Android的:如何设置从按钮编辑文本的内容点击? [英] Android: How to set the contents of a Edit text from a Button click?

查看:608
本文介绍了Android的:如何设置从按钮编辑文本的内容点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我到Android新秀。我想实现在Android中一个简单的计算器来获得在Android的基础知识的保持的。我想显示与数字和数学运算和当用户presses相应号码被显示在编辑文本的按键键盘。我尝试使用gettext的()和更新编辑文本的内容,但它显示了pressed按钮的内容之外。另外我怎么读按钮的内容,以便在code做数学运算?任何帮助将是非常美联社preciated。

I am a rookie to android. I am thinking of implementing a simple calculator in android to get a hold of the basics in android. I want to display a keypad with numbers and mathematical operations and when the user presses the keys the corresponding number is displayed in edit text. I tried using gettext() and updating the contents of edit text but it shows just the contents of pressed button. Also how do I read the contents of button so as to do mathematical operations in code? Any help would be much appreciated.

问候,

原始

推荐答案

处理按钮的标题为实际数据是什么,是pretty多,当你做一个数字,仅按钮可以接受的做法计算器,所以我不知道这是否适用于Android基本信息;)

Treating the caption of the button as the actual data is something that is pretty much only acceptable practice when you're doing the digit-buttons in a calculator, so I don't know if that holds for 'android basics' ;)

不管;

你说明你想要显示相应的号码,当用户presses一个键(按键?)。然后你说的getText 只是说明你的pressed按钮的内容...是,不完全是你问什么?您可能需要提供一点code,并告诉我们什么不工作,你所期望的方式。但是,如果一个按钮有文本'8',而你要正确对待,作为在数学运算八,你需要分析它:

You're stating you want to display the corresponding number, when the user presses a key (button?). And then you say getText just shows you the content of the pressed button... Is that not exactly what you're asking for? You might need to provide a bit of code and show us what's not working the way you intended. But if a button has the text '8', and you want to treat that as an eight in mathematical operations, you need to parse it:

Integer myNumber = Integer.parse(myButton.getText());

...这当然会引发异常的则myButton 不解析为一个整数的文本。

...which will of course throw an exception if the text of myButton does not resolve to an integer.

修改,AS评论

这是你的问题,你在你的评论给的解释是,的setText()完全重置文本它传递的价值,但你可以使用组合那和的getText()如果你只是想添加一些东西到当前值:

From the explanation of your problem you gave in your comment, yes, setText() resets the text entirely to the value it's being passed, but you can use a combination of that and getText() if you just want to append something to the current value:

myEditText.setText( myEditText.getText() + " " + myButton.getText() );

这篇关于Android的:如何设置从按钮编辑文本的内容点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆