Android:如何通过单击按钮设置编辑文本的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2699729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:00:32  来源:igfitidea点击:

Android: How to set the contents of a Edit text from a Button click?

android

提问by Primal Pappachan

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.

我是安卓新手。我正在考虑在 android 中实现一个简单的计算器来掌握 android 中的基础知识。我想显示一个带有数字和数学运算的小键盘,当用户按下按键时,相应的数字会显示在编辑文本中。我尝试使用 gettext() 并更新编辑文本的内容,但它只显示按下按钮的内容。另外我如何读取按钮的内容以便在代码中进行数学运算?任何帮助将非常感激。

regards,

问候,

Primal

原始的

采纳答案by dxh

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' ;)

当您在计算器中执行数字按钮时,将按钮的标题视为实际数据几乎是唯一可以接受的做法,所以我不知道这是否适用于“android 基础知识”;)

Regardless;

不管;

You're stating you want to display the corresponding number, when the user presses a key (button?). And then you say getTextjust 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:

当用户按下某个键(按钮?)时,您表示要显示相应的数字。然后你说getText只显示你按下的按钮的内容......这不正是你要的吗?您可能需要提供一些代码并向我们展示哪些地方没有按照您的预期工作。但是如果一个按钮有文本“8”,并且你想在数学运算中把它当作一个 8,你需要解析它:

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

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

...如果文本myButton未解析为整数,这当然会引发异常。

EDIT, AS OF COMMENT

编辑,作为评论

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:

根据您在评论中对问题的解释,是的,setText()将文本完全重置为其传递的值,但您可以使用它的组合,getText()如果您只想将某些内容附加到当前值:

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

回答by Erich Douglass

To set the contents of an EditText:

设置 EditText 的内容:

EditText text = (EditText) findViewById(R.id.your_text);
text.setText("Display this text");

Instead of trying to pull the text off of your buttons and use it as an integer, I would just add a click listener to each one that "knows" the value of its button:

与其尝试从按钮中提取文本并将其用作整数,我只想为每个“知道”其按钮值的单击侦听器添加一个单击侦听器:

Button button = (Button) findViewById(R.id.num_1);
button.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        // Do something with the value of the button
    }
});

回答by Vicky

Try the following code.

试试下面的代码。

EditText x = (EditText)findviewbyid(R.id.abc);

Don't use x.setText(); instead, use x.append(Your variable or value here). This will not clear the text that came from the previous button.

不要使用x.setText(); 相反,使用x.append(Your variable or value here). 这不会清除来自上一个按钮的文本。

回答by Rajkumari

To Set the text of button in EditText:

在 EditText 中设置按钮的文本:

EditText edittext=(EditText)findviewById(R.id.edittext); 
Button num1=(Button)findViewById(R.id.button_num1);
Button num2=(Button)findviewById(R.id.button_num2);
num1.setOnClickListener(new View.OnClickListener() { 
    @Override public void onClick(View v) {
        edittext.append("1");
    } 
});
num1.setOnClickListener(new View.OnClickListener() { 
    @Override
    public void onClick(View v) {
        edittext.append("2");
    } 
});


for simple calcultaor see hereit will help you to create a calculator in android.

对于简单 的计算器,请参见此处,它将帮助您在 android 中创建计算器。