私はJavaに精通していますが、Androidプログラミングを学んでいます。Hello, LinearLayout の例のカラー バーがかなり気に入ったので、それを 13 か月の子供で使用できる小さなプログラムに拡張しようとしています。ラベルと 4 行で 7 色を表示しています。私がやりたいことの 1 つは (私はバイリンガルなので)、アプリを終了したり、新しいロケールをロードしたり、アプリを再起動したりせずに、その場でロケールの表示言語を変更できるようにすることです (これは機能しますが、面倒です)。そのため、7 つのカラー バーには選択した言語に適したラベルが付けられていますが、プログラムを終了するのではなく、プログラム内をクリックして変更できるようにしたいと考えています。言い換えれば、電話全体ではなく、アプリの範囲内でのみ Locale を変更したいのです。
Locale の変更に関するいくつかのヒントをここで見つけましたが、この例で 100% うまくいくものはありません。私が行ったことは、Spinner コードとレイアウトを組み合わせることです。変数とスコープがすべて機能していることを確認するために、すべてを(今のところ)1つのファイルに保持しようとしています(「公式の」Android HelloSpinnerコードと同じように、物事をやり取りしようとしましたが、何もしませんでしたしかし混乱)。HelloLinearLayout のカスタム バージョン用にこれまでにコーディングした内容は次のとおりです。
どんなアドバイスでも大歓迎です!ああ、私は Gingerbread に対してビルドとテストを行っています。それが私の HTC 電話で実行されているからです。
package net.buellnet.hellolinearlayout;
import java.util.Locale;
import android.app.Activity;<br/>
import android.content.res.Configuration;<br/>
import android.os.Bundle;<br/>
import android.view.View;
import android.widget.AdapterView;<br/>
import android.widget.ArrayAdapter;<br/>
import android.widget.Spinner;<br/>
import android.widget.Toast;<br/>
public class HelloLinearLayoutActivity extends Activity {<br/>
/** Called when the activity is first created. */<br/>
@Override<br/>
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.lang_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setHapticFeedbackEnabled(true);
spinner.setOnItemSelectedListener(null);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String language = parent.getItemAtPosition(pos).toString();
Configuration newConfig = new Configuration();
Toast.makeText(parent.getContext(), "The language is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();<br/>
/* I threw the above line in just to see if the control was working. I can change the "language" but the Toast line never pops up, nor does the locale ever change. */
if (language.equals("English")) {
newConfig.locale = Locale.ENGLISH;
super.onConfigurationChanged(newConfig);
Locale.setDefault(newConfig.locale);
getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
}
else if (language.equals("French")) {
newConfig.locale = Locale.FRENCH;
super.onConfigurationChanged(newConfig);
Locale.setDefault(newConfig.locale);
getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
}
else if (language.equals("German")) {
newConfig.locale = Locale.GERMAN;
super.onConfigurationChanged(newConfig);
Locale.setDefault(newConfig.locale);
getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
}
else if (language.equals("Italian")){
newConfig.locale = Locale.ITALIAN;
super.onConfigurationChanged(newConfig);
Locale.setDefault(newConfig.locale);
getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
}
else if (language.equals("Portuguese")) {
newConfig.locale = new Locale("pt");
super.onConfigurationChanged(newConfig);
Locale.setDefault(newConfig.locale);
getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
}
else if (language.equals("Spanish")) {
newConfig.locale = new Locale("es");
super.onConfigurationChanged(newConfig);
Locale.setDefault(newConfig.locale);
getBaseContext().getResources().updateConfiguration(newConfig, getResources().getDisplayMetrics());
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
</code>