我已经创建了一个广播组,用户可以在其中选择所需的语言,并将应用程序语言更改为所选语言,但是我无法使用功能(不确定如何操作!)
settingsActivity
setAppLocale
函数onRadioButtonClicked
准备改变语言settingsActivity.java
package com.xxxxxx.xxxxx;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RadioButton;
import java.util.Locale;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
//locale settings
public void setAppLocale(String localeCode) {
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
conf.setLocale(new Locale(localeCode.toLowerCase()));
} else {
conf.locale = new Locale(localeCode.toLowerCase());
}
res.updateConfiguration(conf, dm);
}
// application language switch
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_indo:
if (checked)
setAppLocale("id");
break;
case R.id.radio_english:
if (checked)
setAppLocale("en");
break;
}
}
}
settings_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/settings">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="4sp"
android:layout_marginRight="4sp"
android:weightSum="3"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/appLang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="35dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:layout_marginStart="35dp"
android:layout_marginLeft="35dp"
android:orientation="horizontal">
<TextView
android:id="@+id/applangtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/applangtextstring" />
<RadioButton
android:id="@+id/radio_indo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/indoLang" />
<RadioButton
android:id="@+id/radio_english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="@string/englishLang" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
我需要在Java文件中进行2件事:
现在的问题是如何连接
onRadioButtonClicked
到
setAppLocale
?以及返回当前语言onCreate
以显示当前选择的语言?
根据以下答案,这是我的最新更新和添加的其他文件。 但是我的语言切换不起作用
settingsActivity.java
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.util.Locale;
public class SettingsActivity extends AppCompatActivity {
PrefManager prefManager; //added
RadioButton radio_indo, radio_english; //added
RadioGroup appLang; //added
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
//added
prefManager = new PrefManager(this);
radio_indo = findViewById(R.id.radio_indo);
radio_english = findViewById(R.id.radio_english);
appLang = findViewById(R.id.appLang);
if (prefManager.getLanguage().equals("en")) {
radio_english.setChecked(true);
} else {
radio_english.setChecked(true);
}
// application language switch (added)
appLang.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
switch (checkId) {
case R.id.radio_indo:
prefManager.setLanguage("id");
// you need to restart or recreate your activity after locale change
break;
case R.id.radio_english:
prefManager.setLanguage("en");
// you need to restart or recreate your activity after locale change
break;
}
}
});
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
//locale settings
public void setAppLocale(String localeCode) {
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
conf.setLocale(new Locale(localeCode.toLowerCase()));
} else {
conf.locale = new Locale(localeCode.toLowerCase());
}
res.updateConfiguration(conf, dm);
}
// removed my old function as new function added to onCreate
}
PrefManager.java
增补课
import android.content.Context;
import android.content.SharedPreferences;
public class PrefManager {
private SharedPreferences.Editor editor;
private Context mContext;
private SharedPreferences prefs;
private final String LANGUAGE = "language";
private final String PREF = "user_data";
public PrefManager(Context mContext) {
this.mContext = mContext;
}
public String getLanguage() {
this.prefs = this.mContext.getSharedPreferences(PREF, 0);
return this.prefs.getString(LANGUAGE, "en");
}
public void setLanguage(String language) {
this.editor = this.mContext.getSharedPreferences(PREF, 0).edit();
this.editor.putString(LANGUAGE, language);
this.editor.apply();
}
}
BaseActivity.java
增补课
import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
/**
* Created by nilesh on 20/3/18.
*/
public class BaseActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
Locale newLocale;
String lang = new PrefManager(newBase).getLanguage();
if (lang.equals("en")) {
newLocale = new Locale("en");
} else {
newLocale = new Locale(lang);
}
Context context = ContextWrapper.wrap(newBase, newLocale);
super.attachBaseContext(context);
}
}
settings_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/settings">
<RadioGroup
android:id="@+id/appLang"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="90dp"
android:layout_marginEnd="35dp"
android:layout_marginRight="35dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/applangtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/applangtextstring" />
<RadioButton
android:id="@+id/radio_indo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/indoLang" />
<RadioButton
android:id="@+id/radio_english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/englishLang" />
</RadioGroup>
</RelativeLayout>
这就是我所要进行的语言切换。
PS-1:基于答案中提供的解决方案,我应该
BaseActivity
在所有活动中添加
扩展,但是由于我的所有活动都kotlin
没有java
(settingsActivity除外),所以我无法添加它。PS-2:即使没有翻译,是因为我至少不能添加扩展名,我应该能够在我的settingsActivity中看到翻译活动是
java
正确的吗?
知道为什么此开关不起作用吗?
注意
您可以从github repo下载源代码
将当前语言电台输入标记为已选中
然后,您需要将您的区域设置更改标记/状态保存在其中 SharedPreferences
样本代码请 按照下列步骤
创建一个班级名称
PrefManager
import android.content.Context;
import android.content.SharedPreferences;
public class PrefManager {
private SharedPreferences.Editor editor;
private Context mContext;
private SharedPreferences prefs;
private final String LANGUAGE = "language";
private final String PREF = "user_data";
public PrefManager(Context mContext) {
this.mContext = mContext;
}
public String getLanguage() {
this.prefs = this.mContext.getSharedPreferences(PREF, 0);
return this.prefs.getString(LANGUAGE, "en");
}
public void setLanguage(String language) {
this.editor = this.mContext.getSharedPreferences(PREF, 0).edit();
this.editor.putString(LANGUAGE, language);
this.editor.apply();
}
}
现在在您的代码中添加以下代码/条件
settingsActivity.java
public class JavaActivity extends AppCompatActivity {
PrefManager prefManager;
RadioButton radio_indo, radio_english;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java);
prefManager = new PrefManager(this);
radio_indo = findViewById(R.id.radio_indo);
radio_english = findViewById(R.id.radio_english);
if (prefManager.getLanguage().equals("en")) {
radio_english.setChecked(true);
} else {
radio_english.setChecked(true);
}
}
}
当用户选择另一个单选按钮时进行更改
SharedPreferences
注意:您应该使用RadioGroup.OnCheckedChangeListener()
而不是android:onClick="onRadioButtonClicked"
样本代码
public class JavaActivity extends AppCompatActivity {
PrefManager prefManager;
RadioButton radio_indo, radio_english;
RadioGroup appLang;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java);
prefManager = new PrefManager(this);
radio_indo = findViewById(R.id.radio_indo);
radio_english = findViewById(R.id.radio_english);
appLang = findViewById(R.id.appLang);
if (prefManager.getLanguage().equals("en")) {
radio_english.setChecked(true);
} else {
radio_english.setChecked(true);
}
appLang.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
switch (checkId) {
case R.id.radio_indo:
prefManager.setLanguage("id");
// you need to restart or recreate your activity after locale change
break;
case R.id.radio_english:
prefManager.setLanguage("en");
// you need to restart or recreate your activity after locale change
break;
}
}
});
}
}
问题内容: 我正在尝试在Android Studio中使用Java版本1.7,但是很遗憾,它无法正常工作…如果将版本设置为 但是,当我使用带有字符串的声明语句时,出现了在Java 1.7中应该发生的错误,因此我是否需要设置其他设置? 问题答案: 不,没有设置可更改。Android SDK不支持完整的Java 7语法,因此您不能使用它。 请注意,最新的android版本支持java.nio。*(ja
我安装了Java1.7.0_21,卸载了以前的版本后,今天安装了1.7.0_45。现在Android Studio已经停止编译项目,并表示无法找到1.7.0_21文件夹。如何为Java SDK设置新路径?我也改变了环境变量,但不起作用。
我想自定义android studio编辑器背景,所以我想把图像设置为编辑器,有没有可能,有没有什么选项可以执行?
我想做一个应用程序,使用谷歌翻译API。自从我添加了 我的构建中的依赖项。gradle我有个错误: 现在必须显式声明注释处理器。发现编译类路径上的以下依赖项包含注释处理器。请将它们添加到annotationProcessor配置中。-自动值-1.2。jar(com.google.auto.value:auto-value:1.2)或者,设置android。defaultConfig。javaCom
我的SO或Google搜索发现了很多关于Eclipse的资料,但没有关于Android Studio的资料。