视图弹出应用程序启动

时间:2017-03-02 20:19:23

标签: java android xml

我有一个应用程序可以选择在开始时选择语言。目前,当应用程序启动时,我必须单击选择语言,然后弹出显示包含不同的语言。但我不想点击选择语言选项来显示弹出窗口。我希望我的应用程序自动出现一个弹出窗口,以便在应用启动时选择语言。 我按照http://programmerguru.com/android-tutorial/android-localization-example教程。 这是我想要的东西的快照。 click to view the image

以下是Mainactivity.java的代码

2.0

这是main.xml的代码代码

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);
	}
}

1 个答案:

答案 0 :(得分:0)

如果您正在使用材质警报对话框,则必须在启动器活动的onCreate中显示它,以便它是在创建启动器活动后运行的第一个代码。

希望这有帮助。

相关问题