[英]Pop up menu on Application start
我有一个可以在开始时选择语言的应用程序。 当前,当应用程序启动时,我必须单击“选择语言”,然后弹出包含不同语言的窗口。 但我不想单击“选择语言”选项以显示弹出窗口。 我希望我的应用在启动时自动显示弹出窗口以选择语言。 我遵循了本教程。 这是我想要实现的目标的快照。
这是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>
在您的Activity
onCreate()
中尝试一下:
spinnerctrl = (Spinner) findViewById(R.id.spinner1);
findViewById(android.R.id.content).post(new Runnable() {
@Override
public void run() {
spinnerctrl.performClick();
}
});
//Rest of the code...
在Activity
onCreate期间它将自动打开spinner
弹出窗口
更新
取消可运行的使用:
宣布:
Handler handler;
Runnable runnable;
里面onCreate()
handler = new Handler();
runnable = new Runnable() {
public void run() {
spinnerctrl.performClick();
}
};
handler.postDelayed(this, 500);
现在,使用以下命令在onItemSelected
取消它:
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");
handler.removeCallbacks(runnable);
}
//rest of the condition
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.