gpt4 book ai didi

java - Dropdown android - 仅当用户更改其值时才调用 setOnItemSelectedListener

转载 作者:行者123 更新时间:2023-12-04 03:47:43 28 4
gpt4 key购买 nike

我有一个下拉菜单,我在上面设置了一些值,并默认显示用户从共享首选项中保存的最后一个值。它运作良好。问题是当我开始 Activity 时,将这个保存的用户值放在选择上,默认情况下,它调用 setOnItemSelectedListener 并向用户表示 toast 。这次我不想在 setOnItemSelectedListener 中运行 block 代码,就在用户更改下拉值时。关于如何解决它的任何想法?

String defaultValue = pref02.getString("fonte", "Normal");
String[] items = new String[]{"Normal", "10", "12", "14", "16", "18", "20", "24", "28", "30"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
int spinnerPosition = adapter.getPosition(defaultValue);
dropdown.setSelection(spinnerPosition); //select the atual saved value.
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
SharedPreferences pref2 = getApplicationContext().getSharedPreferences("pref02", MODE_PRIVATE);
SharedPreferences.Editor editor = pref2.edit();

String drop = dropdown.getSelectedItem().toString();
editor.putString("fonte", drop);
editor.apply();
Toast.makeText(Config.this, "Saved :)", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

最佳答案

解决方案:使用以下代码(您可以查看注释行以查看指南)

String defaultValue = pref02.getString("fonte", "Normal");
String[] items = new String[]{"Normal", "10", "12", "14", "16", "18", "20", "24", "28", "30"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
int spinnerPosition = adapter.getPosition(defaultValue);
dropdown.setSelection(spinnerPosition); //select the atual saved value.

// Add this block of code
dropdown.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
// Remove listener to make sure it doesn't called later.
dropdown.removeOnLayoutChangeListener(this);

// Set your listener here
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
SharedPreferences pref2 = getApplicationContext().getSharedPreferences("pref02", MODE_PRIVATE);
SharedPreferences.Editor editor = pref2.edit();

String drop = dropdown.getSelectedItem().toString();
editor.putString("fonte", drop);
editor.apply();
Toast.makeText(Config.this, "Saved :)", Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
});

关于java - Dropdown android - 仅当用户更改其值时才调用 setOnItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64893791/

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