[android]Spinner 不应对用户

标签: Java Android
发布时间: 2017/3/19 18:56:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试图 spinner 对作出反应时我按上的项目之一,然而,输入的用户,没有任何反应。不变得任何错误在 IDE 中或在运行期间 time 所以我不知道我做错了什么。谁能帮?

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:

首先,你需要将listener添加到你 spinner 。添加下面这行代码里面 onCreate 方法︰

themeSpinner.setOnItemSelectedListener(this);

下一步 == 运算符返回 false 都有 if 语句。

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

另一方面, .equals() 将比较变量的实际内容。因此,你越来越虚假的 if 语句和无内的代码执行。

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