我的应用中有一个简单的 if-else 语句,但它不起作用。如果我在 Spinner 中选择“天”,则只能编辑该日。它不起作用,因为仍然可以编辑所有日子。有人可以告诉我为什么吗?
final String[] items={"Day","Date","Time","Location"};
mSpin2.setOnItemSelectedListener(
new OnItemSelectedListener() {
@Override
public void onItemSelected(
AdapterView<?> parent2, View view, int pos, long id
) {
String strsel=items[pos];
if(strsel.equalsIgnoreCase("Day")) {
btnChangeDate.setFocusable(false);
btnChangeDate.setEnabled(false);
//btnChangeDate.setClickable(false);
btnChangeTime.setFocusable(false);
btnChangeTime.setEnabled(false);
//btnChangeTime.setClickable(false);
mLatitude.setFocusable(false);
mLatitude.setEnabled(false);
mLongitude.setFocusable(false);
mLongitude.setEnabled(false);
} else {
mMonday.setFocusable(true);
mMonday.setEnabled(true);
mTuesday.setFocusable(true);
mTuesday.setEnabled(true);
mWednesday.setFocusable(true);
mWednesday.setEnabled(true);
mThursday.setFocusable(true);
mThursday.setEnabled(true);
mFriday.setFocusable(true);
mFriday.setEnabled(true);
mSaturday.setFocusable(true);
mSaturday.setEnabled(true);
mSunday.setFocusable(true);
mSunday.setEnabled(true);
}
String strsel2=items[pos];
if(strsel2.equalsIgnoreCase("Date")) {
mMonday.setFocusable(false);
mMonday.setEnabled(false);
mTuesday.setFocusable(false);
mTuesday.setEnabled(false);
mWednesday.setFocusable(false);
mWednesday.setEnabled(false);
mThursday.setFocusable(false);
mThursday.setEnabled(false);
mFriday.setFocusable(false);
mFriday.setEnabled(false);
mSaturday.setFocusable(false);
mSaturday.setEnabled(false);
mSunday.setFocusable(false);
mSunday.setEnabled(false);
btnChangeTime.setFocusable(false);
btnChangeTime.setEnabled(false);
//btnChangeTime.setClickable(false);
mLatitude.setFocusable(false);
mLatitude.setEnabled(false);
mLongitude.setFocusable(false);
mLongitude.setEnabled(false);
} else {
btnChangeDate.setFocusable(true);
btnChangeDate.setEnabled(true);
}
String strsel3=items[pos];
if(strsel3.equalsIgnoreCase("Time")) {
mMonday.setFocusable(false);
mMonday.setEnabled(false);
mTuesday.setFocusable(false);
mTuesday.setEnabled(false);
mWednesday.setFocusable(false);
mWednesday.setEnabled(false);
mThursday.setFocusable(false);
mThursday.setEnabled(false);
mFriday.setFocusable(false);
mFriday.setEnabled(false);
mSaturday.setFocusable(false);
mSaturday.setEnabled(false);
mSunday.setFocusable(false);
mSunday.setEnabled(false);
btnChangeDate.setFocusable(false);
btnChangeDate.setEnabled(false);
//btnChangeDate.setClickable(false);
mLatitude.setFocusable(false);
mLatitude.setEnabled(false);
mLongitude.setFocusable(false);
mLongitude.setEnabled(false);
} else {
btnChangeTime.setFocusable(true);
btnChangeTime.setEnabled(true);
}
String strsel4=items[pos];
if(strsel.equalsIgnoreCase("Location")) {
mMonday.setFocusable(false);
mMonday.setEnabled(false);
mTuesday.setFocusable(false);
mTuesday.setEnabled(false);
mWednesday.setFocusable(false);
mWednesday.setEnabled(false);
mThursday.setFocusable(false);
mThursday.setEnabled(false);
mFriday.setFocusable(false);
mFriday.setEnabled(false);
mSaturday.setFocusable(false);
mSaturday.setEnabled(false);
mSunday.setFocusable(false);
mSunday.setEnabled(false);
btnChangeDate.setFocusable(false);
btnChangeDate.setEnabled(false);
//btnChangeDate.setClickable(false);
btnChangeTime.setFocusable(false);
btnChangeTime.setEnabled(false);
//btnChangeTime.setClickable(false);
} else {
//mLatitude.setFocusable(true);
mLatitude.setEnabled(true);
//mLongitude.setFocusable(true);
mLongitude.setEnabled(true);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {}
}
);
答案 0 :(得分:0)
好的,我再次查看了你的代码。你要么做一些不明显的事情,要么你没有使用else
案例,因为它应该被使用。无论如何,你知道你得到的结果是错误的。我建议在下面的模式之后重写监听器。您可能需要根据实际要求对其进行微调:
static final int dayIndex = 0;
static final int dateIndex = 1;
static final int timeIndex = 2;
static final int locIndex = 3;
public void onItemSelected( AdapterView<?> parent2, View view, int pos, long id ) {
mMonday.setFocusable( pos == dayIndex );
mMonday.setEnabled( pos == dayIndex );
mTuesday.setFocusable( pos == dayIndex );
mTuesday.setEnabled( pos == dayIndex );
mWednesday.setFocusable( pos == dayIndex );
mWednesday.setEnabled( pos == dayIndex );
mThursday.setFocusable( pos == dayIndex );
mThursday.setEnabled( pos == dayIndex );
mFriday.setFocusable( pos == dayIndex );
mFriday.setEnabled( pos == dayIndex );
mSaturday.setFocusable( pos == dayIndex );
mSaturday.setEnabled( pos == dayIndex );
mSunday.setFocusable( pos == dayIndex );
mSunday.setEnabled( pos == dayIndex );
btnChangeDate.setFocusable( pos == dateIndex );
btnChangeDate.setEnabled( pos == dateIndex );
btnChangeTime.setFocusable( pos == timeIndex );
btnChangeTime.setEnabled( pos == timeIndex );
mLatitude.setEnabled( pos == locIndex );
mLongitude.setEnabled( pos == locIndex );
}
如果您在微调器中选择第一个项目(Day),则上面的代码将启用mMonday..mSunday,否则禁用它们。如果你选择第二项(日期)......它会启用btnChangeDate。请注意,如果我猜对了你的意图,并且这段代码有效,那么你对if / then / else结构的理解是非常有缺陷的,你可能会我想读一读。