gpt4 book ai didi

java - 选择带有变量的 Spinner,

转载 作者:行者123 更新时间:2023-11-30 01:49:12 24 4
gpt4 key购买 nike

我是 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)); }




});



}}

最佳答案

好吧,您需要一个适配器来说明您想要的选项。

@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了解更多关于微调器的信息。

关于java - 选择带有变量的 Spinner,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33311824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com