在主要活动中,我向用户显示语言集。
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewSelection"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:gravity="center_horizontal">
<RadioButton
android:id="@+id/english_lang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/langtext_english"
android:textSize="22dp"
android:padding="10dp" />
<RadioButton
android:id="@+id/indonesian_lang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/langtext_indonesian"
android:textSize="22dp"
android:padding="10dp" />
</RadioGroup>
当用户选择一种语言时,我跳转到位于MainActivityFragment.java类中的onRadioButtonClicked()方法
public void onRadioButtonClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.english_lang:
if (checked)
//
break;
case R.id.french_lang:
if (checked)
// Ninjas rule
break;
}
}
在res目录下,我创建了一个名为values-fr的新文件夹,我放置了一个带有法语值的string.xml。
1)我应该如何将用户选择的语言传递给整个应用程序上下文并进一步使用它?
2)为什么我的方法从未使用过?
MainActivityFragment.java
package com.a2ndnumber.a2ndnumber;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
private ArrayAdapter<String> countryList_Adapter;
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
public void onRadioButtonClicked(View view){
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.english_lang:
if (checked)
//
break;
case R.id.indonesian_lang:
if (checked)
// Ninjas rule
break;
}
}
/*
* When user is selecting the country, in background we grab 25 numbers of each country,
* so that we are ready in next fragment where we let them choose the number
* */
public class FetchNumbers_Task extends AsyncTask<Void, Void, Void>{
private final String LOG_TAG = FetchNumbers_Task.class.getSimpleName();
/**
* Override this method to perform a computation on a background thread. The
* specified parameters are the parameters passed to {@link #execute}
* by the caller of this task.
* <p/>
* This method can call {@link #publishProgress} to publish updates
* on the UI thread.
*
* @param params The parameters of the task.
* @return A result, defined by the subclass of this task.
* @see #onPreExecute()
* @see #onPostExecute
* @see #publishProgress
*/
@Override
protected Void doInBackground(Void... params) {
return null;
}
}
}
答案 0 :(得分:1)
您可以使用以下代码更改应用的语言:
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, Youractivity.class); // refresh the activity
refresh.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
refresh.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(refresh);
finish();
}
要获得Radio Buttons的点击监听器,请使用:
mRadioHindi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
Log.d("SettingsActivity::","onCheckedChanged hindi" + b);
PreferenceManager.saveLanguage("hi");
setLocale("hi");
}
}
});
对所有单选按钮使用setOnCheckedChangeListener。
将语言保存为共享首选项,下次当用户打开应用程序时,从sharedPreference获取语言并调用setLocale()方法。