我是编程新手,并且是第一次做这样的事情。我找到了一些例子,但没有这样的例子。
我正在尝试使用带有标志的两个按钮创建一个活动,当应用程序首次安装并运行时会出现。用户,选择他的语言,然后应用程序必须保留选择并每次使用该选择打开它,而不再运行该活动。我已经翻译并制作了一个字符串fr并改变了一些drawables。
问题是,我在开始时有一个Splash活动(每次运行),然后是语言选择活动,之后是WelcomeActivity - 这也是应用程序第一次运行时出现的。
首先,每次应用运行时都会显示我的语言活动,并且不会保留选择。
此外,我正在尝试,当语言活动运行时,不管怎样,为了使WelcomeActivity运行,不将firstrun设置为false。
这是language.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<Button
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_weight="1"
android:id="@+id/fr"
android:background="@drawable/flag_fr"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:clickable="true"
android:orientation="horizontal"
android:layout_gravity="center_vertical"/>
<Button
android:layout_width="wrap_content"
android:layout_height="150dp"
android:layout_weight="1"
android:id="@+id/en"
android:background="@drawable/flag_en"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:clickable="true"
android:orientation="horizontal"
android:layout_gravity="center_vertical"/>
</LinearLayout>
这是我的Language.java
public class Language extends Activity{
SharedPreferences sharedPref = null;
SharedPreferences.Editor editor;
Button fr,en;
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.language);
sharedPref = getSharedPreferences("com.myAppName", MODE_PRIVATE);
Intent refresh = new Intent(this, WelcomeActivity.class);
startActivity(refresh);
}
@Override
protected void onResume() {
super.onResume();
fr = (Button) findViewById(R.id.fr);
en = (Button) findViewById(R.id.en);
// Checking for first time launch - before calling setContentView()
if (sharedPref.getBoolean("firstrun", true)) {
fr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setLocale("fr");
}
});
en.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setLocale("en");
}
});
sharedPref.edit().putBoolean("firstrun", false).commit();
}
}
public void setLocale(String lang) {
Locale 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, WelcomeActivity.class);
finish();
startActivity(refresh);
}
这是我的清单
<activity
android:name=".WelcomeActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.default" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="MainActivity"></activity>
<activity android:name=".Language"
android:label="Language"/>
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
我确信这很简单,但我找不到它。谢谢你!