我正在构建一个拥有20多个微调器的应用程序。我正在尝试一键重置所有微调器。那可能吗?您能否指导我如何实现这一目标。下面是我的代码。
如果可能,我希望从菜单中选择重置(右上角3个点),有人可以帮助我。先感谢您 :)
package com.example.hfacs_test09;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class Unsafe extends Fragment implements OnItemSelectedListener {
private Spinner uhs1a,uhs1b,uhs1c;
private TextView unsaferesult;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.unsafe, container, false);
unsaferesult = (TextView) rootView.findViewById(R.id.unsaferesult);
uhs1a = (Spinner) rootView.findViewById(R.id.uhs1a);
uhs1b = (Spinner) rootView.findViewById(R.id.uhs1b);
uhs1c = (Spinner) rootView.findViewById(R.id.uhs1c);
uhs1a.setOnItemSelectedListener(this);
uhs1b.setOnItemSelectedListener(this);
uhs1c.setOnItemSelectedListener(this);
return rootView;
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id){
//uhs1a.setSelection(position);
int r1 = Integer.valueOf((String) uhs1a.getSelectedItem());
int r2 = Integer.valueOf((String) uhs1b.getSelectedItem());
int r3 = Integer.valueOf((String) uhs1c.getSelectedItem());
int total = r1 + r2 + r3;
//int myresult = (Integer) uhs1a.getSelectedItem() ;
unsaferesult.setText("Total Score is " + total);
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
最佳答案
要将微调器重置为默认值:
uhs1a = (Spinner) rootView.findViewById(R.id.uhs1a); // Ignore this if you already did that in onCreateView
uhs1a.setSelection(0); // Assuming the default position is 0.
要为所有微调器执行此操作,或者为每个微调器手动执行此操作,或者将所有微调器添加到ArrayList,然后
for(int i=0; i < myArrayList.size(); i++)
myArrayList.get(i).setSelection(0);
要通过“菜单”(3个点)按钮进行操作,请执行以下操作:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.your_menu_xml_file, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.reset_button:
// Reset spinners here.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
关于android - 如何将多个微调器值重置为默认值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20921126/