我試圖製作一個簡單的計算器,但由於某些原因,我無法獲得結果。
該應用程序運行,但當我按下按鈕沒有彈出結果顯示,如預期。Android Studio中的應用程序問題
下面是代碼:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText number1EditText;
private EditText number2EditText;
private Button divideButton;
private Button multiplyButton;
private Button addButton;
private Button subtractButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1EditText = (EditText) findViewById(R.id.number1EditText);
number2EditText = (EditText) findViewById(R.id.number2EditText);
divideButton=(Button)findViewById(R.id.divideButton);
multiplyButton=(Button)findViewById(R.id.multiplyButton);
addButton=(Button)findViewById(R.id.addButton);
subtractButton=(Button)findViewById(R.id.subtractButton); }
public void onClick(View v){
switch (v.getId()){
case R.id.divideButton:
division();
break;
case R.id.multiplyButton:
multiplication();
break;
case R.id.addButton:
addition();
break;
case R.id.subtractButton:
subtraction();
break;} }
private void division(){
String number1 = number1EditText.getText().toString();
String number2 = number2EditText.getText().toString();
int answer = Integer.parseInt(number1)/Integer.parseInt(number2);
Toast.makeText(this,String.valueOf(answer), Toast.LENGTH_LONG).show();}
private void multiplication(){
String number1=number1EditText.getText().toString();
String number2=number2EditText.getText().toString();
int answer=Integer.parseInt(number1)*Integer.parseInt(number2);
Toast.makeText(this,String.valueOf(answer),Toast.LENGTH_LONG).show(); }
private void addition(){
String number1=number1EditText.getText().toString();
String number2=number2EditText.getText().toString();
int answer = Integer.parseInt(number1) + Integer.parseInt(number2);
Toast.makeText(this,String.valueOf(answer), Toast.LENGTH_LONG).show(); }
private void subtraction(){
String number1=number1EditText.getText().toString();
String number2=number2EditText.getText().toString();
int answer=Integer.parseInt(number1) - Integer.parseInt(number2);
Toast.makeText(this,String.valueOf(answer),Toast.LENGTH_LONG).show(); }}
請添加相關的XML佈局代碼 – Zoe