这里有我的java代码
private EditText screen; //textbox screen
private float numberBf; // save screen before pressing button operation
private String operation;
private ButtonClickListener btnClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen = (EditText) findViewById(editText);
int idList[] = {R.id.button0,R.id.button,R.id.button2,R.id.button3,R.id.button4,
R.id.button5,R.id.button6,R.id.button7,R.id.button8,R.id.button9, R.id.buttonAdd,R.id.buttonMul,
R.id.buttonC, R.id.buttonDiv,R.id.buttonDot,R.id.buttonEq,R.id.buttonSub};
for(int id:idList){
View v = (View) findViewById(id);
v.setOnClickListener(btnClick);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//new class
private class ButtonClickListener implements View.OnClickListener {
public void onClick(View view){
switch (view.getId()){
case R.id.buttonC: //clear button
screen.setText("0");
numberBf = 0;
operation = "";
break;
//adding function
case R.id.buttonAdd:
mMath("+");
break;
case R.id.buttonSub:
mMath("-");
break;
case R.id.buttonMul:
mMath("*");
break;
case R.id.buttonDiv:
mMath("/");
break;
case R.id.buttonEq:
mResult();
break;
default:
String num = ((Button) view).getText().toString();
getKeyboard(num);
break;
}
}
}
public void mMath(String s){
numberBf = Float.parseFloat(screen.getText().toString()); //save the screen
operation = s; //save operation
screen.setText("0"); //clear screen
}
public void mResult(){
float numberFl = Float.parseFloat(screen.getText().toString());
float result = 0;
if(operation.equals("+"))
result = numberFl + numberBf;
if (operation.equals("-"))
result = numberBf - numberFl;
if(operation.equals("*"))
result = numberFl * numberBf;
if (operation.equals("/"))
result = numberBf / numberFl;
screen.setText(String.valueOf(result));
}
public void getKeyboard(String str){
String scrCurrent = screen.getText().toString();
if (scrCurrent.equals("0"))
scrCurrent = "";
scrCurrent = str;
screen.setText(str);
}
}
我的activity_main xml代码的一部分,第9个按钮和文本位置是
activity_main :
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:text="0"
android:layout_below="@+id/button0"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="57dp"
android:gravity="right"
android:editable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/button9"
android:layout_below="@+id/button5"
android:layout_toRightOf="@+id/button5"
android:textStyle="bold" />
请帮助我谢谢
答案 0 :(得分:0)
这应该有效:
screen = (EditText) findViewById(R.id.editText);
答案 1 :(得分:0)
screen = (EditText) findViewById(editText);
参数editText不是有效的资源标识符。在XML中你应该有
<EditText ...
android:id="@+id/thetextid">
然后在你的代码中你应该
screen = (EditText) findViewById(R.id.thetextid);
答案 2 :(得分:0)
您尚未实例化按钮单击侦听器。添加
btnClick = new ButtonClickListener();
在任何setOnClickListener(btnClick)
来电之前。