如何在 Android Studio 的微调器中使用 "group" if 语句?
How do I "group" if statements to use in a spinner in Android Studio?
我是 Java/Android Studio 的新手,遇到问题 "grouping"?在微调器中使用的 if 语句。基本上我需要我的应用程序根据微调器设置的位置使用某些值。这是我当前的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//Instantiating Widgets
firstInput = findViewById(R.id.firstInput);
secondInput = findViewById(R.id.secondInput);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
spinner = findViewById(R.id.spinner);
//Spinner - adapter etc.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.grades, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
//Adding a click event for button (Executing the convert method when clicked)
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int force = 0;
double in1 = Double.valueOf(firstInput.getText().toString());
double in2 = Double.valueOf(secondInput.getText().toString());
//Grade 5 Fine Thread
if (in1 / in2 == 0.25) {
force = 2325;
} else if (in1 / in2 == 0.3125) {
force = 3675;
} else if (in1 / in2 == 0.375) {
force = 5588;
} else if (in1 / in2 == 0.4375) {
force = 7575;
} else if (in1 / in2 == 0.5) {
force = 10200;
} else if (in1 / in2 == 0.5625) {
force = 12975;
} else if (in1 / in2 == 0.625) {
force = 16350;
} else if (in1 / in2 == 0.75) {
force = 23775;
} else if (in1 / in2 == 0.875) {
force = 32475;
}
//Grade 5 Coarse Thread
if(in1 / in2 == 0.25) {
force = 2025;
} else if(in1 / in2 == 0.3125) {
force = 3338;
} else if(in1 / in2 == 0.375){
force = 4950;
} else if(in1 / in2 == 0.4375){
force = 6788;
} else if(in1 / in2 == 0.5){
force = 9075;
} else if(in1 / in2 == 0.5625){
force = 11625;
} else if(in1 / in2 == 0.625){
force = 14400;
} else if(in1 / in2 == 0.75){
force = 21300;
} else if(in1 / in2 == 0.875){
force = 29475;
}
textView.setText(Double.toString(((in1 / in2) * 0.2 * force) / 12));
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
if (position == 0) {
}
if (position == 1) {
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
因此,如果位置设置为 0,我需要将力设置为细螺纹值,如果位置设置为 1,则需要将力设置为粗螺纹值。我不知道该去哪里。任何帮助是极大的赞赏。
谢谢。
如果我误解了这个问题,请告诉我,但我认为这对你有用:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double in1 = Double.valueOf(firstInput.getText().toString());
double in2 = Double.valueOf(secondInput.getText().toString());
if (spinner.getSelectedItemPosition() == 0) {
//fine thread code
...
} else {
//coarse thread code
...
}
textView.setText(Double.toString(((in1 / in2) * 0.2 * force) / 12));
}
}
我是 Java/Android Studio 的新手,遇到问题 "grouping"?在微调器中使用的 if 语句。基本上我需要我的应用程序根据微调器设置的位置使用某些值。这是我当前的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//Instantiating Widgets
firstInput = findViewById(R.id.firstInput);
secondInput = findViewById(R.id.secondInput);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
spinner = findViewById(R.id.spinner);
//Spinner - adapter etc.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.grades, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
//Adding a click event for button (Executing the convert method when clicked)
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int force = 0;
double in1 = Double.valueOf(firstInput.getText().toString());
double in2 = Double.valueOf(secondInput.getText().toString());
//Grade 5 Fine Thread
if (in1 / in2 == 0.25) {
force = 2325;
} else if (in1 / in2 == 0.3125) {
force = 3675;
} else if (in1 / in2 == 0.375) {
force = 5588;
} else if (in1 / in2 == 0.4375) {
force = 7575;
} else if (in1 / in2 == 0.5) {
force = 10200;
} else if (in1 / in2 == 0.5625) {
force = 12975;
} else if (in1 / in2 == 0.625) {
force = 16350;
} else if (in1 / in2 == 0.75) {
force = 23775;
} else if (in1 / in2 == 0.875) {
force = 32475;
}
//Grade 5 Coarse Thread
if(in1 / in2 == 0.25) {
force = 2025;
} else if(in1 / in2 == 0.3125) {
force = 3338;
} else if(in1 / in2 == 0.375){
force = 4950;
} else if(in1 / in2 == 0.4375){
force = 6788;
} else if(in1 / in2 == 0.5){
force = 9075;
} else if(in1 / in2 == 0.5625){
force = 11625;
} else if(in1 / in2 == 0.625){
force = 14400;
} else if(in1 / in2 == 0.75){
force = 21300;
} else if(in1 / in2 == 0.875){
force = 29475;
}
textView.setText(Double.toString(((in1 / in2) * 0.2 * force) / 12));
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
if (position == 0) {
}
if (position == 1) {
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
因此,如果位置设置为 0,我需要将力设置为细螺纹值,如果位置设置为 1,则需要将力设置为粗螺纹值。我不知道该去哪里。任何帮助是极大的赞赏。
谢谢。
如果我误解了这个问题,请告诉我,但我认为这对你有用:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double in1 = Double.valueOf(firstInput.getText().toString());
double in2 = Double.valueOf(secondInput.getText().toString());
if (spinner.getSelectedItemPosition() == 0) {
//fine thread code
...
} else {
//coarse thread code
...
}
textView.setText(Double.toString(((in1 / in2) * 0.2 * force) / 12));
}
}