そのような状況の例を挙げます。
アプリケーションには、標準の英語とロシア語の2つの言語があります。 このアプリケーションは、デバイスがウクライナ語であるウクライナ人によってインストールされることに決めましたが、彼はロシア語もよく知っていますが、英語はあまり知りません。 しかし、アプリケーションにウクライナ語がないことを発見したAndroidは、標準の言語でアプリケーションを起動しますが、これは私たちの状況では英語ですが、アプリケーションをロシア語で実行するにはシステム言語を変更する必要がありますが、これはあまり良くありません。
ここでは、このような多くの同様の状況に対して、自動言語選択、英語、ロシア語などを含む言語選択項目を設定に表示する解決策があります。 (必要なものに応じて)。
書き始めましょう。
1. Applicationクラスを作成し、android:name = ""パラメーターの対応するアプリケーションセクションのマニフェストで定義する必要があります。
例えば:
<application android:icon="@drawable/icon" android:label="@string/app_name" android:name="MyApplication" android:enabled="true"> </application>
(ただし、一部のクラスには既にこのクラスがあります)
2.言語を選択して設定を作成します。これを行うには、設定ファイルに以下を追加します。
<ListPreference android:key="lang" android:title="@string/LangTitle" android:summary="@string/LangSummary" android:entries="@array/entries_lang" android:entryValues="@array/entryvalues_lang" android:dialogTitle="@string/LangDialogTitle" />
3.次の行を使用して、必要な行をファイルに追加します。
<string name="LangTitle"></string> <string name="LangDialogTitle"> :</string> <string name="LangSummary"> .\n .</string>
4.そして、配列2のテキスト配列を持つファイルで:
<string-array name="entries_lang"> <item> </item> <item></item> <item></item> </string-array> <string-array name="entryvalues_lang"> <item>default</item> <item>en</item> <item>ru</item> </string-array>
5.作成されたクラスのonCreateメソッドで、文字列変数「lang」を宣言し、設定から変数を読み取り、アプリケーションの構成を変更し、構成が変更されたときに呼び出されるメソッドを変更します) その結果、次のクラスを取得します。
public class MyApplication extends Application { private SharedPreferences preferences; private Locale locale; private String lang; @Override public void onCreate() { preferences = PreferenceManager.getDefaultSharedPreferences(this); lang = preferences.getString("lang", "default"); if (lang.equals("default")) {lang=getResources().getConfiguration().locale.getCountry();} locale = new Locale(lang); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, null); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); locale = new Locale(lang); Locale.setDefault(locale); Configuration config = new Configuration(); config.locale = locale; getBaseContext().getResources().updateConfiguration(config, null); } }
6.その後、言語を適用するために、アプリケーションを完全に再起動する必要があります(finish()。アクティビティを再起動するだけなので、ここでは役に立ちません)。これにはSystem.exit()コマンドを使用します。
(この例では、アラームに再起動ポイントを作成しました)。
7.ネットワークの問題を回避するために、ローカライズを使用する各アクティビティのマニフェストで推奨されます。
android:configChanges="locale"
また、アプリケーションが正しく表示されるように、サポートされている画面サイズを決定します。
<supports-screens android:anyDensity="true" android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" />
このような簡単な方法で、ユーザーの生活を簡素化できます。
このアプローチの利点:
-ユーザーには言語の選択が与えられます。
短所:
-追加された言語を配列に指定する必要があります。
いつものように、ソースコードを使用したサンプルアプリケーション:
1. 例のソースコード 。
2. サンプルアプリケーション(apk) 。
PS System.exit()を除いて、アプリケーション(アプリケーションを含む)を完全に閉じることができる他の方法はありますか?