【发布时间】:2017-03-02 20:19:23
【问题描述】:
我有一个应用程序,它可以在开始时选择语言。目前,当应用程序启动时,我必须单击选择语言,然后弹出包含不同语言的窗口。但我不想点击选择语言选项来显示弹出窗口。我希望我的应用程序在应用程序启动时自动出现一个弹出窗口以选择语言。 我跟着http://programmerguru.com/android-tutorial/android-localization-example 教程。 这是我想要的东西的快照。 click to view the image
这是 Mainactivity.java 的代码
public class AndroidLocalize extends Activity {
Spinner spinnerctrl;
Button btn;
Locale myLocale;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinnerctrl = (Spinner) findViewById(R.id.spinner1);
spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if (pos == 1) {
Toast.makeText(parent.getContext(),
"You have selected Tamil", Toast.LENGTH_SHORT)
.show();
setLocale("ta");
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
"You have selected Hindi", Toast.LENGTH_SHORT)
.show();
setLocale("hi");
} else if (pos == 3) {
Toast.makeText(parent.getContext(),
"You have selected English", Toast.LENGTH_SHORT)
.show();
setLocale("en");
}
else if (pos == 4) {
Toast.makeText(parent.getContext(),
"You have selected Arabic", Toast.LENGTH_SHORT)
.show();
setLocale("ar");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
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, AndroidLocalize.class);
startActivity(refresh);
}
}
这是main.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/greet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/greet"
android:textSize="25sp" android:gravity="center" android:paddingTop="25sp" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/sachin" android:paddingTop="25sp" />
<TextView
android:textColor="#ffffff"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/langselection"
android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" android:paddingTop="25sp"/>
<Spinner
android:popupBackground="#ff004372"
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/languages"
android:gravity="center" android:paddingTop="25sp" />
</LinearLayout>a
【问题讨论】:
-
有人可以回答吗?