我对编码非常陌生,开始学习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));
}
});
答案 0 :(得分:2)
就像@NickL一样,您的click事件应该在方法中。您可以像这样直接将其放置在onCreate
中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
//Do what you want with the click here
}
});
}
或者您可以像这样在onCreate
中调用您的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myCoolButtonMethod();
}
private void myCoolButtonMethod(){
Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
//Do what you want with the click here
}
});
}
您还错误地声明了方法,在您的情况下,它看起来像这样:
public class MainActivity extends AppCompatActivity {
EditText firstNumber;
EditText secondNumber;
TextView addResult;
Button btnAdd;
double num1,num2,sum;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNumber = findViewById(R.id.txtNumber1);
secondNumber = findViewById(R.id.txtNumber2);
addResult = findViewById(R.id.txtResult);
btnAdd = findViewById(R.id.btnAdd);
myCoolButtonMethod();
}
private void myCoolButtonMethod(){
Button clickButton = (Button) findViewById(R.id.clickButton);
clickButton.setOnClickListener( new OnClickListener() {
@Override
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));
}
});
}
答案 1 :(得分:0)
这样做: 将onClick方法放入onCreate。
ngFor
答案 2 :(得分:0)
首先,您必须将视图声明更改为
secondNumber = (EditText) findViewById(R.id.action_save);
addResult = (TextView) findViewById(R.id.action_save);
btnAdd = (Button) findViewById(R.id.action_save);
然后将double变量更改为final。 而且你必须放
btnAdd.setOnClickListener(new View.OnClickListener() {
内部
protected void onCreate(Bundle savedInstanceState) {
阻止方法。
这是完整的代码:
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;
import com.example.android.pets.R;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
EditText firstNumber;
EditText secondNumber;
final TextView addResult;
Button btnAdd;
final double num1, num2, sum;
firstNumber = (EditText) findViewById(R.id.action_save);
secondNumber = (EditText) findViewById(R.id.action_save);
addResult = (TextView) findViewById(R.id.action_save);
btnAdd = (Button) findViewById(R.id.action_save);
num1 = Double.parseDouble(firstNumber.getText().toString());
num2 = Double.parseDouble(secondNumber.getText().toString());
sum = num1 + num2;
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addResult.setText(Double.toString(sum));
}
});
}
}