gpt4 book ai didi

android - 如何将多个微调器值重置为默认值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:53:39 24 4
gpt4 key购买 nike

我正在构建一个包含 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/

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