android中onItemSelected微调器的问题
Posted
技术标签:
【中文标题】android中onItemSelected微调器的问题【英文标题】:Problem with onItemSelected spinner in android 【发布时间】:2020-05-12 12:49:33 【问题描述】:我想通过选定的微调器更改我的应用程序的语言,但第一次不要更改。经过两次选择,它工作。我强制使用 setOnTouchListener 因为没有它会发生循环。它工作但经过多次选择。
这是我的代码:
boolean spinnerTouch=false;
Spinner s1;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
if (spinnerTouch)
String text = parent.getItemAtPosition(position).toString();
if (text.equals(getString(R.string.persian)))
lang = getResources().getString(R.string.lang_fa);
country = getResources().getString(R.string.country_ir);
onConfigurationChanged(new Configuration());
else if (text.equals(getString(R.string.english)))
lang = getResources().getString(R.string.lang_en);
country = getResources().getString(R.string.country_us);
onConfigurationChanged(new Configuration());
Utility.setPreferences(this, getResources().getString(R.string.lang), lang);
Utility.setPreferences(this, getResources().getString(R.string.country), country);
spinnerTouch = false;
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
s1 = (Spinner) findViewById(R.id.spnLang);
s1.setOnItemSelectedListener(this);
s1.setOnTouchListener(new View.OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
spinnerTouch= true;
return false;
);
@Override
public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
if (lang != null)
Utility.languageHelper(lang,country,newConfig,getBaseContext().getResources());
recreate();
public class Utility
public static void languageHelper(String lang, String country, Configuration config, Resources resources)
Locale locale = new Locale(lang,country);
config.locale = locale;
Locale.setDefault(locale);
resources.updateConfiguration(config, resources.getDisplayMetrics());
【问题讨论】:
在微调器中选择语言后尝试重新启动您的应用 但重启应用前不要更改配置 你从哪里打电话给languageHelper
和recreate()
?
希望这个对你有所帮助:***.com/questions/59854390/…
@Md.Asaduzzaman 非常感谢。确实解决了。
【参考方案1】:
请试试这个,
String[] weeks = "Sunday", "Monday", "Tuesday", "Wednesday", "thursday";
public synchronized void showOptionDialogFilter(String title, final List<String> itemList, final FilterPreferenceListener optionSelected)
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(html.fromHtml("<b>" + title + "</b>"));// bold style of title of dialog
String[] listData = new String[itemList.size()];
listData = itemList.toArray(listData);
// add a list
builder.setItems(listData, new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
optionSelected.selectedItem(itemList.get(which),which);
);
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
单击称为 spinnerSelect 的微调器
public void spinnerSelect()
showOptionDialogFilter("Select Value", Arrays.asList(weeks), new FilterPreferenceListener()
@Override
public void selectedItem(String item,int position)
editText.setText(item);
);
对结果使用接口回调
public interface FilterPreferenceListener
void selectedItem(String item,int pos);
【讨论】:
【参考方案2】:在ConfiguratiojChange下面添加类
public class ConfigurationChange
/**
* Author:Hardik Talaviya
* Date: 2019.08.3 2:30 PM
* Describe:
*/
private Context context;
String lang = "EN";
private Locale locale = null;
public ConfigurationChange(Context context)
this.context = context;
public ContextWrapper changeLang(Context context,String lang)
this.context = context;
Configuration config = context.getResources().getConfiguration();
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang))
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
conf.setLocale(locale);
else
conf.locale = locale;
context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics());
return new ContextWrapper(context);
现在在您的活动中添加以下代码
private boolean isFirstTime = true;
//add below into onCreate
language_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
if (isFirstTime)
isFirstTime = false;
else
ConfigurationChange configurationChange = new ConfigurationChange(YourActivity.this);
configurationChange.changeLang(YourActivity.this,yourSelectedLanguageCode);//pass selected language code like "EN","FR" etc.
Intent intent = getIntent();
finish();
startActivity(intent);
@Override
public void onNothingSelected(AdapterView<?> parent)
);
希望对你有帮助!
谢谢。
【讨论】:
感谢您的建议,但是如何在我的班级中导入 PrefManager?未知 PrefManager。 使用默认的安卓偏好。 你要试试这个吗? 谢谢。这个***.com/a/59855148/12078052解决了我的问题 好的,但是试试我的答案,它也是最好的,请尝试一下。以上是关于android中onItemSelected微调器的问题的主要内容,如果未能解决你的问题,请参考以下文章
使用 Onitemselect 侦听器从微调器调用另一个活动。 [关闭]
Spin ner OnItemSelected在Android Studio中不起作用[关闭]
微调器:当所选项目保持不变时,不调用 onItemSelected