赞
踩
二、新建values-en-rUS和values-zh-rCN文件夹,并且在两个文件夹中都新建一个strings.xml文件
三、values、values-en-rUS和values-zh-rCN文件夹中的strings.xml文件内容如下所示:
将android:configChanges="locale"添加到AndroidManifest文件中,具体如下代码所示:
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:configChanges="locale"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/Theme.BookAdvancedLight">
- <activity android:name=".activities.test.I18nTestActivity"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
values -- strings.xml:
- <resources>
- <string name="app_name" translatable="false">BookAdvancedLight</string>
- <string name="username">username</string>
- <string name="password">password</string>
- <string name="login">login</string>
- <string name="usernameHint">please input username</string>
- <string name="passwordHint">please input password</string>
- </resources>
values-en-rUS -- strings.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <resources xmlns:tools="http://schemas.android.com/tools"
- tools:ignore="ExtraTranslation">
- <string name="username">username</string>
- <string name="password">password</string>
- <string name="login">login</string>
- <string name="usernameHint">please input username</string>
- <string name="passwordHint">please input password</string>
- </resources>
values-zh-rCN -- strings.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <resources xmlns:tools="http://schemas.android.com/tools"
- tools:ignore="ExtraTranslation">
- <string name="username" >用户名</string>
- <string name="password">密码</string>
- <string name="login">登录</string>
- <string name="usernameHint">请输入用户名</string>
- <string name="passwordHint">请输入密码</string>
- </resources>
Activity文件:
- public class I18nTestActivity extends AppCompatActivity {
-
- private Button login;
- private ToggleButton translate;
- private TextView txtUsername,txtPassword;
- private EditText editUsername,editPassword;
-
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_i18n_test);
- InitView();
- }
-
- private void InitView() {
- login = findViewById(R.id.bt_login);
- txtUsername = findViewById(R.id.txt_username);
- txtPassword = findViewById(R.id.txt_password);
- editUsername = findViewById(R.id.edit_username);
- editPassword = findViewById(R.id.edit_password);
- translate = findViewById(R.id.toggle_button);
- translate.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String sta= getResources().getConfiguration().locale.getLanguage();
- translateText(sta);
- }
- });
- }
-
- public void translateText(String sta){
- if (sta.equals("zh")){
- Resources resources = this.getResources();// 获得res资源对象
- Configuration config = resources.getConfiguration();// 获得设置对象
- DisplayMetrics dm = resources.getDisplayMetrics();// 获得屏幕参数:主要是分辨率,像素等。
- config.locale = Locale.US; // 英文
- resources.updateConfiguration(config, dm);
- this.recreate();
- }else {
- //转换为中文
- Resources resources = this.getResources();// 获得res资源对象
- Configuration config = resources.getConfiguration();// 获得设置对象
- DisplayMetrics dm = resources.getDisplayMetrics();// 获得屏幕参数:主要是分辨率,像素等。
- config.locale = Locale.SIMPLIFIED_CHINESE; // 英文
- resources.updateConfiguration(config, dm);
- this.recreate();
- }
- }
-
- }
activity.xml 文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".activities.test.I18nTestActivity"
- android:orientation="vertical">
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="80dp"
- android:orientation="horizontal">
-
- <TextView
- android:id="@+id/txt_username"
- android:text="@string/username"
- android:gravity="center"
- android:layout_width="60dp"
- android:layout_height="70dp"
- />
-
- <EditText
- android:id="@+id/edit_username"
- android:layout_width="match_parent"
- android:layout_height="70dp"
- android:hint="@string/usernameHint"
- />
-
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="80dp"
- android:orientation="horizontal">
-
- <TextView
- android:id="@+id/txt_password"
- android:text="@string/password"
- android:gravity="center"
- android:layout_width="60dp"
- android:layout_height="70dp"
- />
-
- <EditText
- android:id="@+id/edit_password"
- android:layout_width="match_parent"
- android:layout_height="70dp"
- android:hint="@string/passwordHint"
- />
-
- </LinearLayout>
-
-
- <Button
- android:id="@+id/bt_login"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/login"/>
-
- <ToggleButton
- android:layout_gravity="center"
- android:layout_marginTop="10dp"
- android:id="@+id/toggle_button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textOn="中文"
- android:textOff="English"
- android:textAllCaps="false"/>
-
- </LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。