0

我的应用程序中有一个简单的if-else语句,但它不起作用。如果我在Spinner中选择“ day ” ,则只能编辑那一天。它不起作用,因为仍然可以编辑所有日子。有人能告诉我为什么吗?

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

1 回答 1

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

如果您在微调器中选择第一项(日),上面的代码将启用 mMonday..mSunday,否则禁用它们。如果您选择第二项(日期)...等,它将启用 btnChangeDate。请注意,如果我猜对了您的意图,并且此代码有效,那么您对 ​​if/then/else 构造的理解存在很大缺陷,您可能想读一读。

于 2012-12-26T23:47:28.783 回答