所以我为游戏制作了一个统计计算器应用程序,用户通过各种微调器选择组合,然后单击按钮来计算得到的统计数据。所以现在我想完全删除按钮,并在微调器中的项目发生更改时自动更新结果统计信息。这是我的微调器监听器代码:
public class MySpinnerListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int identifier = arg0.getId();
String id = arg0.getItemAtPosition(arg2).toString();
id=id.replaceAll("\\s+","");
Data data= Data.valueOf(id);
switch (data) {
//big case switch to find what is clicked an update the variables accordingly..
这是我的计算功能
public void Calculate(){
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//creates object with all the stats (there are two because the user can compare different combinations) ChangeColor just makes it look nice.
TotalStats stats1 = new TotalStats(player, car, wheel, glide);
TotalStats stats2 = new TotalStats(player2, car2, wheel2,
glide2);
ChangeColor leftStats= new ChangeColor(stats1);
ChangeColor rightStats= new ChangeColor(stats2);
//outputs the stats
statsView1.setText(leftStats.builder, BufferType.SPANNABLE);
statsView2.setText(rightStats.builder,BufferType.SPANNABLE);
}
});
编辑:这是我的onCreate方法和我的初始设置。
Spinner character, character1;
Spinner vehicle, vehicle1;
Spinner wheels, wheels1;
Spinner glider, glider1;
Button calculate;
TextView statsView1, statsView2, statsView, title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fragment_main);
statsView = (TextView) findViewById(R.id.statsView);
statsView1 = (TextView) findViewById(R.id.stats1);
statsView2 = (TextView) findViewById(R.id.stats2);
title=(TextView)findViewById(R.id.title);
Typeface font = Typeface.createFromAsset(getAssets(),"New Super Mario Font U.ttf");
statsView.setTypeface(font);
statsView1.setTypeface(font);
statsView2.setTypeface(font);
title.setTypeface(font);
InitialSetupUI();
Calculate();
}
private void InitialSetupUI() {
// TODO Auto-generated method stub
character = (Spinner) findViewById(R.id.character);
vehicle = (Spinner) findViewById(R.id.vehicle);
wheels = (Spinner) findViewById(R.id.wheels);
glider = (Spinner) findViewById(R.id.glider);
character1 = (Spinner) findViewById(R.id.character2);
vehicle1 = (Spinner) findViewById(R.id.vehicle2);
wheels1 = (Spinner) findViewById(R.id.wheels2);
glider1 = (Spinner) findViewById(R.id.glider2);
calculate = (Button) findViewById(R.id.calculate);
ArrayAdapter cadapter = ArrayAdapter.createFromResource(this,
R.array.character_array, R.layout.spinner_item);
ArrayAdapter vadapter = ArrayAdapter.createFromResource(this,
R.array.vehicle_array, R.layout.spinner_item);
ArrayAdapter wadapter = ArrayAdapter.createFromResource(this,
R.array.wheels_array, R.layout.spinner_item);
ArrayAdapter gadapter = ArrayAdapter.createFromResource(this,
R.array.glider_array, R.layout.spinner_item);
character.setAdapter(cadapter);
vehicle.setAdapter(vadapter);
wheels.setAdapter(wadapter);
glider.setAdapter(gadapter);
character1.setAdapter(cadapter);
vehicle1.setAdapter(vadapter);
wheels1.setAdapter(wadapter);
glider1.setAdapter(gadapter);
vehicle.setOnItemSelectedListener(new MySpinnerListener());
character.setOnItemSelectedListener(new MySpinnerListener());
wheels.setOnItemSelectedListener(new MySpinnerListener());
glider.setOnItemSelectedListener(new MySpinnerListener());
vehicle1.setOnItemSelectedListener(new MySpinnerListener());
character1.setOnItemSelectedListener(new MySpinnerListener());
wheels1.setOnItemSelectedListener(new MySpinnerListener());
glider1.setOnItemSelectedListener(new MySpinnerListener());
}