gpt4 book ai didi

java - 旋转器没有对用户使用react

转载 作者:行者123 更新时间:2023-12-02 03:03:05 25 4
gpt4 key购买 nike

我试图让旋转器对用户输入使用react,但是,当我按下其中一个项目时,什么也没有发生。我在 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 变量和“if 语句中的任何字符串” 并不引用相同的对象或内存。

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

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

关于java - 旋转器没有对用户使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42178553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com