【问题标题】:How to reset multiple spinner values to default如何将多个微调器值重置为默认值
【发布时间】:2014-01-22 03:42:30
【问题描述】:

我正在构建一个拥有 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) {

}
}

【问题讨论】:

    标签: android spinner default-value


    【解决方案1】:

    将微调器重置为默认值:

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

    【讨论】:

    • 感谢您提供准确的信息!遵循您的代码后它可以工作。我真的很感谢你的时间。再次感谢您。
    • @Alexis 不客气!请接受这个答案作为正确答案。如果您需要更多帮助,请询问我。谢谢! :-)
    【解决方案2】:

    同单旋。

    spinner.setSelection(position);
    

    只需对按钮方法的 onclick 中的所有微调器执行此操作即可。

    【讨论】:

    • 感谢您的回复:)
    【解决方案3】:

    your_spinner.setSelection(0);
    // 它会将您的微调器设置为位置 1。
    // 如果传递 1,它将设置到位置 2.....

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多