如何存储按钮的按钮文本中的变量? Android的计算器项目 [英] How to store Button Text of Buttons in a variable? Android Calculator Project
本文介绍了如何存储按钮的按钮文本中的变量? Android的计算器项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在Android计算器,而现在在Java中,当然,蚀。在我的计划,我希望能够存储在Android按钮作为操作员(theOperator)的文字来检验这样我就可以以此为基础进行一些code。我有我的所有按钮设置为使用XML的一些文字。这里有一个例子:
<按钮
机器人:ID =@ + ID / bMultiply
机器人:layout_width =65dp
机器人:layout_height =65dp
机器人:layout_alignParentRight =真
机器人:layout_below =@ + ID / bSix
////机器人:文本=*/////就在这里,按钮的文本设置。
机器人:TEXTSIZE =40dp
安卓的onClick =的onClick
/>
现在,这里是我的主要code:
包rechee.cool;
进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
公共类HelloAndroidActivity延伸活动{
/ **当第一次创建活动调用。 * /
公众的EditText显示;
双共1 = 0;
双共2 = 0;
焦炭theOperator;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
//按钮变量与XML关联参考
显示=(EditText上)findViewById(R.id.editText1);
}
。字符串display1 = display.getText()的toString();
//我要存储在Android按钮btnText的按钮上的文字。我该怎么做呢?
公共无效getOperator(字符串btnText){
theOperator = btnText.charAt(0);
双displayValue = Double.parseDouble(display1);
共1 + = displayValue;
display.setText();
}
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.bOne:
display.append(1);
打破;
案例R.id.bTwo:
display.append(2);
打破;
案例R.id.bThree:
display.append(3);
打破;
案例R.id.bFour:
display.append(4);
打破;
案例R.id.bFive:
display.append(5);
打破;
案例R.id.bSix:
display.append(6);
打破;
案例R.id.bSeven:
display.append(7);
打破;
案例R.id.bEight:
display.append(8);
打破;
案例R.id.bNine:
display.append(9);
打破;
案例R.id.bZero:
display.append(0);
打破;
案例R.id.bPoint:
display.append(。);
打破;
案例R.id.bClear:
display.setText();
打破;
案例R.id.bAdd:
getOperator(display1);
//字符串theOperator =新的String(+);
打破;
案例R.id.bEqual:
}
}
}
解决方案
要得到一个按钮上的文字。
把一个变量在你的类
公共类HelloAndroidActivity延伸活动{
私人字符串buttonText;
私人Button按钮;
...
然后在onCreate方法获得按钮和文本:
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
按钮=(按钮)findViewById(R.id.bMultiply);
buttonText = button.getText()的toString()。
}
好运气
I've been working on an Android Calculator for a while now in Java, of course, eclipse. In my program, I want to be able to store the text of the android buttons as an an operator (theOperator) to test so I can do some code based on that. I have all my buttons set as some text using xml. Here's an example:
<Button
android:id="@+id/bMultiply"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/bSix"
//// android:text="*" ///// Right here is where the text of the button is set.
android:textSize="40dp"
android:onClick="onClick"
/>
Now here's my main code:
package rechee.cool;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class HelloAndroidActivity extends Activity {
/** Called when the activity is first created. */
public EditText display;
double total1=0;
double total2=0;
char theOperator;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Associate the button variable with the xml reference
display= (EditText) findViewById(R.id.editText1);
}
String display1= display.getText().toString();
// I want to store the button text of the android button as btnText. How do I do this?
public void getOperator(String btnText){
theOperator = btnText.charAt(0);
double displayValue= Double.parseDouble(display1);
total1+=displayValue;
display.setText("");
}
public void onClick(View v) {
switch(v.getId()){
case R.id.bOne:
display.append("1");
break;
case R.id.bTwo:
display.append("2");
break;
case R.id.bThree:
display.append("3");
break;
case R.id.bFour:
display.append("4");
break;
case R.id.bFive:
display.append("5");
break;
case R.id.bSix:
display.append("6");
break;
case R.id.bSeven:
display.append("7");
break;
case R.id.bEight:
display.append("8");
break;
case R.id.bNine:
display.append("9");
break;
case R.id.bZero:
display.append("0");
break;
case R.id.bPoint:
display.append(".");
break;
case R.id.bClear:
display.setText("");
break;
case R.id.bAdd:
getOperator(display1);
//String theOperator= new String("+");
break;
case R.id.bEqual:
}
}
}
解决方案
To get the text of a button.
put a variable in your class
public class HelloAndroidActivity extends Activity {
private String buttonText;
private Button button;
...
then in the onCreate method get the button and text:
@Override
public void onCreate(Bundle savedInstanceState) {
button = (Button)findViewById(R.id.bMultiply);
buttonText = button.getText().toString();
}
good luck
这篇关于如何存储按钮的按钮文本中的变量? Android的计算器项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文