Spinner没有对用户做出反应

时间:2017-02-11 16:53:23

标签: java android android-spinner

我正在尝试让微调器对用户输入作出反应,但是当我按下其中一个项目时,没有任何反应。我在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();
    }
}

1 个答案:

答案 0 :(得分:2)

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

themeSpinner.setOnItemSelectedListener(this);

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

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

另一方面,.equals()将比较变量的实际内容。因此,您通过if语句得到了错误,并且没有执行任何代码。

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