gpt4 book ai didi

java - 让微调器显示他们选择的内容

转载 作者:行者123 更新时间:2023-12-01 14:32:51 24 4
gpt4 key购买 nike

我的spinner可以工作,但我想知道用户在单击我设置的确认按钮之前选择了什么。我猜测类似将 spinners 设置为 variable 这样我就可以在任何地方使用它们,因为到目前为止它们只能在主体中使用。任何帮助将不胜感激

public class Calculate extends Activity implements View.OnClickListener
{
int number = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.calculate);

Spinner spinner1 = (Spinner) findViewById(R.id.hemsSpinner);
Spinner spinner2 = (Spinner) findViewById(R.id.materialSpinner);

Button butn1 = (Button) findViewById(R.id.calculateButton);
butn1.setOnClickListener(this);


// Selection of the spinner
// Application of the Array to the Spinner

ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,Hems.Hem);
spinnerArrayAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
spinner1.setAdapter(spinnerArrayAdapter1);

ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,Material.Material);
spinnerArrayAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(spinnerArrayAdapter2);


EditText qty = (EditText)findViewById(R.id.qtyEditText);
double quantity = qty.getId();

EditText H = (EditText)findViewById(R.id.heightEditText);
double hight = H.getId();

EditText W = (EditText)findViewById(R.id.widthEditText);
double width = W.getId();


}
@Override
public void onClick(View v)
{


if(Calculate.spinner1.equals(Hems.Hem[0]))
{
number = 0;
}
else if (spinner1.equals(Hems.Hem[1]))
{
number = 1;
}
else if (spinner1.equals(Hems.Hem[2]))
{
number = 2;
}
else if (spinner1.equals(Hems.Hem[3]))
{
number = 3;
}


if (v.getId() == R.id.calculateButton)
{
TextView text = (TextView)findViewById(R.id.textView1);
text.setText(Hems.Hem[number]);

TextView dude = (TextView)findViewById(R.id.textView2);
dude.setText((int) Hems.hemPrice[number]);
}
}
}

最佳答案

试试这个

将您的微调器声明为字段

  Spinner spinner1,spinner2;

然后在你的onCreate中像这样获取他们的引用

spinner1 = (Spinner) findViewById(R.id.hemsSpinner);
spinner2 = (Spinner) findViewById(R.id.materialSpinner);

并使用 yourspinner.getSelectedItem() 方法在按下确认按钮时获取微调器数据

关于java - 让微调器显示他们选择的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16719359/

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