言語変更ボタンを使用してヒンディー語と英語を切り替えるときに、アプリケーションのロケール(言語)をプログラムで変更するように変更したい。Android - ボタンクリック時に手動でアプリケーションロケールを変更する
言語を変更するコードがありますが、setContentView()メソッドの前にあるアクティビティのonCreate()を呼び出すと機能します。
ご迷惑をおかけして申し訳ありません。
言語変更ボタンを使用してヒンディー語と英語を切り替えるときに、アプリケーションのロケール(言語)をプログラムで変更するように変更したい。Android - ボタンクリック時に手動でアプリケーションロケールを変更する
言語を変更するコードがありますが、setContentView()メソッドの前にあるアクティビティのonCreate()を呼び出すと機能します。
ご迷惑をおかけして申し訳ありません。
、あなたのアプリケーションは、(デフォルトの文字列に加えて)英語 とアラビア語の文字列の両方をサポートしたい場合は、単に二つの追加 リソースを作成することができ 、ここに私の答え例えば
Android Font in more then one langauge on single screen
を参照してください。 /res/values-en
(英語のstrings.xmlの場合)および /res/values-ar
(アラビア文字の場合はxs.xml)というディレクトリがあります。
strings.xml
ファイル内では、 のリソース名は同じです。また
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">مرحبا في اللغة الإنجليزية</string>
</resources>
:/res/values-ar/strings.xmlファイルは次のようになり、一方で
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello in English!</string>
</resources>
:
例えば、/res/values-en/strings.xml
ファイルが このようになります。 、/res/values-ur_IN/strings.xmlファイルはurduの場合は次のようになります。
インド向けur_INパキスタンのur_PK
文字列を表示/ resを/レイアウトディレクトリのデフォルトのレイアウトファイルは、文字列リソースがどの言語やディレクトリ に関係なく、変数名@文字列/ハローによって 文字列を指し<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">انگریزی میں خوش!!</string>
</resources>
。
Androidオペレーティングシステムは、実行時にロードする文字列(フランス語、英語、またはデフォルト)のバージョンを と決定します。文字列を表示するのTextViewコントロール とレイアウトは次のようになります。文字列は、通常の方法でプログラムによってアクセスされ
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hello" >
</LinearLayout>
:変更の
String str = getString(R.string.hello);
を言語は、あなたがその変更を好きに必要lang ..
btn_english.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show();
}
});
btn_arbice.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(this, getResources().getString(R.string.lbl_langSelecURdu), Toast.LENGTH_SHORT).show();
}
});
btn_urdu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Locale locale = new Locale("ur_IN");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Toast.makeText(HomeActivity.this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show();
}
});
を呼び出すのではなく、推奨されていないので、config.localeとupdateConfigurationの代わりになるもの – ruselli
このボタンをクリックしてみてください。
public void changeLocale()
{
Resources res = getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("hi_IN");
res.updateConfiguration(conf, dm);
setContentView(R.layout.xxx);
}
SharedPrefrencesを使用すると、onCreate()ではなくonStart()でユーザーのプリファレンスと呼び出しのロケールを保存できます。アプリの文句を言わないの現金Iは、ユーザーの選択に従ってヒンディー語または英語にsharedPrefrences値を設定し
public static final String Default="en"; //so if value isn't found then english language will be used.
protected void onStart() {
SharedPreferences sharedPreferences = this.getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
String pine = sharedPreferences.getString("language", DEFAULT);
String languageToLoad = pine;
Locale locale = new Locale(languageToLoad);//Set Selected Locale
Locale.setDefault(locale);//set new locale as default
Configuration config = new Configuration();//get Configuration
config.locale = locale;//set config locale as selected locale
this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics());
invalidateOptionsMenu();
setTitle(R.string.app_name);
super.onStart();
}
ように私は、デフォルト値を定義 ためにこれがうまく働きました。この場合スイッチを使用しました。下にコードを見ることができます。
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (aSwitch.isChecked()) {
SharedPreferences hisharedPreferences = getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
SharedPreferences.Editor hieditor = npsharedPreferences.edit();
npeditor.putString("language","hi");
npeditor.commit();
aSwitch.setChecked(true);
Toast.makeText(Settings.this, "Hindi Language Selected", Toast.LENGTH_LONG).show();
} else {
SharedPreferences ensharedPreferences = getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
SharedPreferences.Editor eneditor = ensharedPreferences.edit();
eneditor.putString("language","en");
eneditor.commit();
Toast.makeText(Settings.this, "English Language Selected", Toast.LENGTH_LONG).show();
aSwitch.setChecked(false);
}
}
});
これが助けになりました!もしあなたが何かのステップで立ち往生したら、私に尋ねてみてください!
アクティビティーを再起動する必要はありませんか?ロケールを変更すると、すぐにアプリのすべてのテキストが更新されるだけではありません –
@ cricket_007はい、これは私がやっていることですが、私はそこにもっと良い解決策があると思っていました。 – User16119012
私が気づいていないのは、すべて手動でsetTextにメソッド –