我的微调器中有一些项目(字符串),当我选择其中一个项目时,我想要一个相关的值,例如:
如果我选择SAE 1020,则返回250.0 如果我选择E-155,则返回300.0
该值可以在EditText中显示,但我看到的唯一值是250.0。 毕竟,这个值应该发送到下一个Activity(我也不知道我是否正确地进行了传输)。
发生了什么事?我该怎么办?
代码:
package com.mateus.ligacoestubulares;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class Dados extends AppCompatActivity {
String [] AçoMontante = {"SAE 1020", "E-155"};
String [] AçoBanzo = {"SAE 1020", "E-155"};
String [] EspessuraT1 = {"0,75 mm", "0,90 mm", "0,95 mm"};
String [] EspessuraTo = {"0,75 mm", "0,90 mm", "0,95 mm"};
EditText campoFy1;
EditText campoFyo;
EditText campoT1;
EditText campoTo;
EditText normalM;
EditText normalB;
EditText momentoB;
EditText teta;
Button next;
Intent intentNext;
Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dados);
Spinner spinnerMont = (Spinner) findViewById(R.id.spinnerM);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AçoMontante);
spinnerMont.setAdapter(arrayAdapter1);
campoFy1 = (EditText) findViewById(R.id.fy1);
String montStr = spinnerMont.getSelectedItem().toString();
String fy1 = Double.toString(choiceM(montStr));
campoFy1.setText(fy1);
Spinner spinnerBanzo = (Spinner) findViewById(R.id.spinnerB);
ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AçoBanzo);
spinnerBanzo.setAdapter(arrayAdapter2);
campoFyo = (EditText) findViewById(R.id.fyo);
String banzoStr = spinnerBanzo.getSelectedItem().toString();
String fyo = Double.toString(choiceB(banzoStr));
campoFyo.setText(fyo);
next = (Button) findViewById(R.id.prosseguir);
intentNext = new Intent(Dados.this, ConferenciaDosDados.class);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String resistenciaM = campoFy1.getText().toString();
String resistenciaB = campoFyo.getText().toString();
bundle.putString("fy1",resistenciaM);
bundle.putString("fyo",resistenciaB);
intentNext.putExtras(bundle);
startActivity(intentNext);
}
});
}
public double choiceM(String str) {
Double f1 = 0.0;
if (str.equals(AçoMontante[0])) {
f1 = 250.0;
} else if (str.equals(AçoMontante[1])) {
f1 = 300.0;
}
return f1;
}
public double choiceB(String str) {
Double fo = 0.0;
if (str.equals(AçoMontante[0])) {
fo = 250.0;
} else if (str.equals(AçoMontante[1])) {
fo = 300.0;
}
return fo;
}
}
答案 0 :(得分:0)
试试这个。此代码按您的意愿运行。
String [] AçoMontante = {"SAE 1020", "E-155"};
String [] AçoBanzo = {"SAE 1020", "E-155"};
String [] EspessuraT1 = {"0,75 mm", "0,90 mm", "0,95 mm"};
String [] EspessuraTo = {"0,75 mm", "0,90 mm", "0,95 mm"};
EditText campoFy1;
EditText campoFyo;
Button next;
Intent intentNext;
Bundle bundle;
String montStr,banzoStr, f1,fo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
campoFy1 = (EditText) findViewById(R.id.fy1);
campoFyo = (EditText) findViewById(R.id.fyo);
Spinner spinnerMont = (Spinner) findViewById(R.id.spinnerM);
Spinner spinnerBanzo = (Spinner) findViewById(R.id.spinnerB);
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, AçoMontante);
ArrayAdapter<String> arrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, AçoBanzo);
spinnerMont.setAdapter(arrayAdapter1);
spinnerBanzo.setAdapter(arrayAdapter2);
spinnerMont.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
montStr = (String) parent.getItemAtPosition(i);
if(montStr == "SAE 1020"){
f1 = "250.0";
}
else if (montStr == "E-155"){
f1 = "300.0";
}
else{}
campoFy1.setText(f1);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
spinnerBanzo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
banzoStr = (String) parent.getItemAtPosition(i);
if(banzoStr == "SAE 1020"){
fo = "250.0";
}
else if (banzoStr == "E-155"){
fo = "300.0";
}
else{}
campoFyo.setText(fo);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
next = (Button) findViewById(R.id.prosseguir);
intentNext = new Intent(MainActivity.this, Second.class);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String resistenciaM = campoFy1.getText().toString();
String resistenciaB = campoFyo.getText().toString();
bundle.putString("fy1",resistenciaM);
bundle.putString("fyo",resistenciaB);
intentNext.putExtras(bundle);
startActivity(intentNext);
}
});
}
此外,您可以使用私有静态变量来传递值,而不是捆绑包
将f1
和fo
设为
private static String f1,fo;
代替String f1,fo;
并在另一项活动中将其称为Dados.f1
或Dados.fo
[这里是当前活动的Dados]