我正在尝试制作一个基本的Android计算器。当我按下编辑文字显示的按钮数字,但没有什么是幸福的。谢谢你的帮助。
这是我的MainActivity.java` 公共类MainActivity扩展了AppCompatActivity {
private EditText Scr;
private float NumerBf;
private String Operation;
private ButtonClickListener btnClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Scr=(EditText)findViewById(R.id.editText3);
Scr.setEnabled(false);
int idList[]={R.id.button0,R.id.button1,R.id.button1,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.buttonC,R.id.buttonDivide,
R.id.buttonEqu,R.id.buttonMinus,R.id.buttonMultiply};
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.menu_main, menu);
return true;
}
public void mMath(String str){
NumerBf=Float.parseFloat(Scr.getText().toString());
Operation =str;
Scr.setText("0");
}
public void getKeyboard(String str){
String SrcCurrent = Scr.getText().toString();
if(SrcCurrent.equals("0"))
SrcCurrent="";
SrcCurrent +=str;
Scr.setText(SrcCurrent);
}
public void mResult(){
float NumAf=Float.parseFloat(Scr.getText().toString());
float result=0;
if(Operation.equals("+")){
result=NumAf+NumerBf;
}
else if(Operation.equals("-")){
result=NumerBf-NumAf;
}
else if(Operation.equals("*")){
result=NumAf*NumerBf;
}
else if(Operation.equals("/")){
result=NumerBf/NumAf;
}
Scr.setText(String.valueOf(result));
}
private class ButtonClickListener implements View.OnClickListener {
public void onClick(View v){
switch(v.getId()){
case R.id.buttonC: //ekranı temizle
Scr.setText("0");
NumerBf=0;
Operation="";
break;
case R.id.buttonAdd:
mMath("+");
break;
case R.id.buttonMinus:
mMath("-");
break;
case R.id.buttonMultiply:
mMath("*");
break;
case R.id.buttonDivide:
mMath("/");
break;
case R.id.buttonEqu:
mResult();
break;
default:
String numb= ((Button) v).getText().toString();
getKeyboard(numb);
break;
}
}
}
`
这是我的content_main.xml`
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/button3"
android:textStyle="bold"
android:layout_above="@+id/button5"
android:layout_toRightOf="@+id/button5"
android:layout_toEndOf="@+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/buttonC"
android:textStyle="bold"
android:longClickable="true"
android:layout_alignTop="@+id/button3"
android:layout_alignRight="@+id/buttonDivide"
android:layout_alignEnd="@+id/buttonDivide"
android:layout_alignLeft="@+id/buttonDivide"
android:layout_alignStart="@+id/buttonDivide" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/button1"
android:textStyle="bold"
android:layout_above="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/button4"
android:textStyle="bold"
android:layout_above="@+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/button6"
android:textStyle="bold"
android:layout_below="@+id/button3"
android:layout_toRightOf="@+id/button5"
android:layout_toEndOf="@+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/button5"
android:textStyle="bold"
android:layout_above="@+id/button8"
android:layout_toRightOf="@+id/button4"
android:layout_toEndOf="@+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/button8"
android:textStyle="bold"
android:layout_above="@+id/button0"
android:layout_toRightOf="@+id/button7"
android:layout_toEndOf="@+id/button7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/button7"
android:textStyle="bold"
android:layout_below="@+id/button5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/button9"
android:textStyle="bold"
android:layout_alignTop="@+id/button8"
android:layout_toRightOf="@+id/button8"
android:layout_toEndOf="@+id/button8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/button0"
android:textStyle="bold"
android:layout_above="@+id/buttonMinus"
android:layout_alignLeft="@+id/button8"
android:layout_alignStart="@+id/button8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/buttonAdd"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/buttonMinus"
android:textStyle="bold"
android:layout_alignTop="@+id/buttonAdd"
android:layout_alignLeft="@+id/button0"
android:layout_alignStart="@+id/button0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/buttonMultiply"
android:textStyle="bold"
android:layout_alignBottom="@+id/buttonMinus"
android:layout_toRightOf="@+id/buttonMinus"
android:layout_toEndOf="@+id/buttonMinus" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/buttonDivide"
android:textStyle="bold"
android:layout_alignTop="@+id/buttonMultiply"
android:layout_toRightOf="@+id/buttonMultiply"
android:layout_toEndOf="@+id/buttonMultiply" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="@+id/buttonEqu"
android:textStyle="bold"
android:layout_below="@+id/buttonMinus"
android:layout_alignLeft="@+id/buttonMinus"
android:layout_alignStart="@+id/buttonMinus" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toLeftOf="@+id/button3"
android:gravity="right"
android:text="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/button2"
android:textStyle="bold"
android:layout_alignBottom="@+id/button3"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3" />
`
答案 0 :(得分:1)
ButtonClickListener btnClick未初始化。您正在设置null onClickListener。因此按下按钮无效