我是Android新手。我试图选择微调器选择并将其乘以用户输入。 (这是一个应用程序,将比萨饼的数量乘以每个披萨的成本)这有意义吗?所以,我能够处理一个微调器选项并获得正确的答案,但我无法弄清楚如何编写多个微调器选项。
继承人Java:
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.Spinner;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
//below is where I have placed the values associated with the Pizza Choices//
//each one is under the txtPizza string array and is referenced//
double costCheese = 4.50;
double costPepperoni = 5.75;
double costVeggie = 4.75;
double costGluten = 5.30;
double costPersonal = 3.15;
int txtnumber;
double txtresult;
String txtGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText amount = (EditText)findViewById(R.id.txtnumber);
final Spinner type =(Spinner)findViewById(R.id.txtGroup);
Button cost = (Button)findViewById(R.id.OrderBtn);
cost.setOnClickListener(new View.OnClickListener() {
final TextView result = ((TextView)findViewById(R.id.txtResult));
@Override
public void onClick(View v) {
txtnumber = Integer.parseInt(amount.getText().toString());
txtresult = costCheese * txtnumber;
DecimalFormat currency = new DecimalFormat("$###,###.##");
result.setText("Cost for this order is " + currency.format(txtresult)); }
});
}}
答案 0 :(得分:1)
你需要一个适配器来说明你想要的选项。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
array_spinner=new String[5];
array_spinner[0]="option1";
array_spinner[1]="option2";
array_spinner[2]="option3";
array_spinner[3]="option4";
array_spinner[4]="option5";
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
}
检查this以了解有关微调器的更多信息。