gpt4 book ai didi

java - 如何根据现在的月份检查选择的季节是否正确?

转载 作者:行者123 更新时间:2023-12-01 19:55:02 25 4
gpt4 key购买 nike

我目前正在开发一个应用程序,它会询问我现在是什么季节。我已经为所有四个季节制作了单选按钮,只是想知道如何根据现在的月份检查他们选择的季节是否正确。顺便说一下,它在一个 fragment 上。

这是我现在的代码。

public class FragmentTwo extends Fragment {

private RadioButton choice1, choice2, choice3, choice4;
private int winter,spring,summer,fall;
private RadioGroup radioGroup;
private int season;


public FragmentTwo() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
choice1 = getView().findViewById(R.id.choiceOne);
choice2 = getView().findViewById(R.id.choiceTwo);
choice3 = getView().findViewById(R.id.choiceThree);
choice4 = getView().findViewById(R.id.choiceFour);
radioGroup = getView().findViewById(R.id.radiogroup);
season = Calendar.MONTH;

choice1.toString();
choice2.toString();
choice3.toString();
choice4.toString();

// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment_two, container, false);


}

}

我还不确定我想做什么。我愿意接受建议。我现在的想法是为每个季节创建一个包含整数的数组,例如:

summer[] = {11,0,1}

这代表十二月、一月、二月。然后找到一种方法将其与当前月份的季节进行比较。但我不确定如何将该数组值添加到单选按钮以检查最终用户是否检查了正确的季节?如果这是有道理的..

最佳答案

将事件监听器添加到单选组并在更改时执行操作:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//create a string array expressing the season each month belongs to
String monthSeasons[] = {
"Winter", "Winter", //0,1
"Spring", "Spring", "Spring",//2,3,4
"Summer", "Summer", "Summer", //5,6,7
"Fall", "Fall", "Fall",//8,9,10
"Winter"//11
};

//identify current month
java.util.Date date= new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
//Verify the current season
System.out.println("This is the current season:"+ monthSeasons[ month ];
//now get the season checked and compare

}
});

关于java - 如何根据现在的月份检查选择的季节是否正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59046169/

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