有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java微调器不响应用户

我试图让微调器对用户输入做出反应,但是,当我按下其中一个项目时,什么也没发生。我在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, 安卓.R.layout.simple_list_item_1 , themes);
    adapter.setDropDownViewResource(安卓.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) 个答案

  1. # 1 楼答案

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

    themeSpinner.setOnItemSelectedListener(this);
    

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

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

    另一方面,.equals()将比较变量的实际内容。因此,通过if语句得到的结果是false,并且没有执行其中的任何代码

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