微调器没有对用户做出反应

Spinner not reacting to user

我试图让微调器对用户输入做出反应,但是,当我按下其中一项时,没有任何反应。我在 IDE 或 运行 期间没有收到任何错误,所以我不知道我做错了什么。有人可以帮忙吗?

public class settings extends AppCompatActivity implements  AdapterView.OnItemSelectedListener {
String selected;
int themeno;
String [] themes = {"Green with blue (Default)", "Green with red", "Green with orange", "Green with yellow", "Green with green", "Green with pink", "Green with purple"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("Settings", Context.MODE_PRIVATE);

    themeno = sharedPref.getInt("Theme", 1);

    Spinner themeSpinner = (Spinner) findViewById(R.id.spinner);

    if(themeno == 1){
        setTheme(R.style.AppTheme);
        themeSpinner.setSelection(1);
    } else if (themeno == 2){
        setTheme(R.style.AppTheme2);
        themeSpinner.setSelection(2);
    } else if (themeno == 3){
        setTheme(R.style.AppTheme3);
        themeSpinner.setSelection(3);
    } else if (themeno == 4){
        setTheme(R.style.AppTheme4);
        themeSpinner.setSelection(4);
    }  else if (themeno == 5){
        setTheme(R.style.AppTheme5);
        themeSpinner.setSelection(5);
    } else if (themeno == 6){
        setTheme(R.style.AppTheme6);
        themeSpinner.setSelection(6);
    } else if (themeno == 7){
        setTheme(R.style.AppTheme7);
        themeSpinner.setSelection(7);
    }



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(settings.this, android.R.layout.simple_list_item_1 , themes);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    themeSpinner.setAdapter(adapter);
}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("Settings", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    selected = parent.getItemAtPosition(position).toString();
    Toast toast = Toast.makeText(getApplicationContext(), "Theme selected. Colours will change when you close settings.", Toast.LENGTH_SHORT);

    if(selected == "Green with blue (Default)"){
        editor.putInt("Theme", 1);
        toast.show();
    }

    if(selected ==  "Green with red"){
        editor.putInt("Theme", 2);
        toast.show();
    }

    if(selected == "Green with orange"){
        editor.putInt("Theme", 3);
        toast.show();
    }

    if(selected == "Green with yellow"){
        editor.putInt("Theme", 4);
        toast.show();
    }

    if(selected == "Green with green"){
        editor.putInt("Theme", 5);
        toast.show();
    }

    if(selected == "Green with pink"){
        editor.putInt("Theme", 6);
        toast.show();
    }

    if(selected == "Green with purple"){
        editor.putInt("Theme", 7);
        toast.show();
    }
}

首先,您需要为微调器添加一个侦听器。在 onCreate 方法中添加这行代码:

themeSpinner.setOnItemSelectedListener(this);

接下来,== 运算符在所有 if 语句中返回 false

您应该使用.equals() 方法来比较字符串。 == 运算符检查对象是否引用相同的内存位置。在您的情况下, selected 变量和 "any of strings in if statement" 不是指同一对象或内存。

另一方面,.equals()会比较变量的实际内容。因此,您通过 if 语句得到 false,并且执行了内部代码的 none。

if("Green with blue (Default)".equals(selected)) {
    editor.putInt("Theme", 1);
    toast.show();
}