我对编码非常陌生,开始学习android。我在Java代码(我的activity.java)中遇到一些小语法错误。请在此方面帮助我。程序是将两个数字相加并显示结果
package com.example.apple.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText firstNumber;
EditText secondNumber;
TextView addResult;
Button btnAdd;
double num1,num2,sum;
firstNumber = findViewById(R.id.txtNumber1);
secondNumber = findViewById(R.id.txtNumber2);
addResult = findViewById(R.id.txtResult);
btnAdd = findViewById(R.id.btnAdd);
}
btnAdd.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 + num2;
addResult.setText(Double.toString(sum));
}
});
相关分类