我需要添加4个不同的数字,这些数字将由用户在4个不同的EditTexts中引入。我已经制作了一个代码,但我不知道如何将这4个数字添加到只有一个,有人可以帮助我吗?这是代码:
import android.content.Intent;
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.Toast;
public class Paquetes extends AppCompatActivity implements View.OnClickListener{
Button solicitar;
Button volver;
EditText n1;
EditText n2;
EditText n3;
EditText n4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paquetes);
solicitar = (Button)findViewById(R.id.button_solicitar);
solicitar.setOnClickListener(this);
volver = (Button)findViewById(R.id.button2_v);
volver.setOnClickListener(this);
n1 = (EditText)findViewById(R.id.n1);
n2 = (EditText)findViewById(R.id.n2);
n3 = (EditText)findViewById(R.id.n3);
n4 = (EditText)findViewById(R.id.n4);
}
@Override
public void onClick (View v){
switch (v.getId()) {
EditText uno = n1.getText();
EditText dos = n2.getText();
EditText tres = n3.getText();
EditText cuatro = n4.getText();
public int n{
n = uno + dos + tres + cuatro;
}
case R.id.button_solicitar:
Toast.makeText(Paquetes.this, "¡Tu pedido ha sido solicitado!", Toast.LENGTH_LONG).show();
startActivity(new Intent(this, Casa.class));
break;
case R.id.button2_v:
startActivity(new Intent(this, Casa.class));
break;
}
}
}
提前致谢,
答案 0 :(得分:0)
您需要将编辑文本中的每个int解析为整数。
int uno = Integer.parseInt(n1.getText().toString());
int dos = Integer.parseInt(n2.getText().toString());
int tres = Integer.parseInt(n3.getText().toString());
int cuatro = Integer.parseInt(n4.getText().toString());
int sum = uno + dos + tres + cuatro;
答案 1 :(得分:0)
// declare (globally) a collection to hold your EditText widgets
private ArrayList<EditText> values = new ArrayList<>();
// Add your EditText widgets to a collection (ArrayList chosen here) in the onCreate method after they are all initialized.
values.add(n1);
values.add(n2);
values.add(n3);
values.add(n4);
// complete your method in the onClick event to mimic the following
// preferably move this method outside onClick
public int addValues(ArrayList<EditText> edts) {
int sum = 0;
for (int i = 0; i < edts.length; i++) {
sum += Integer.parseInt(edts.get[i].getText().toString());
}
return sum;
}
// call your method on a button click
addValues(values);
你的onClick方法应该类似于:
@Override
public void onClick (View v){
switch (v.getId()) {
case R.id.button_solicitar:
addValues(values); // use the value returned by this method call however you want
Toast.makeText(Paquetes.this, "¡Tu pedido ha sido solicitado!", Toast.LENGTH_LONG).show();
startActivity(new Intent(this, Casa.class));
break;
case R.id.button2_v:
startActivity(new Intent(this, Casa.class));
break;
}
}
答案 2 :(得分:-1)
int sum =(
Integer.getInteger(uno.getText()。toString())+ Integer.getInteger(dos.getText()。toString())+ Integer.getInteger(tres.getText()。的toString())+ Integer.getInteger(cuatro.getText()。的toString())
);