如何在android APP中设置系统语言

如何在android APP中设置系统语言,第1张

参考下面代码

private void setLanguage(int language) {

try {

Configuration config = getResources().getConfiguration()

DisplayMetrics dm = getResources().getDisplayMetrics()

if (language == Common.LANGUAGE_EN) {

config.locale = Locale.ENGLISH

} else {

config.locale = Locale.SIMPLIFIED_CHINESE

}

getResources().updateConfiguration(config, dm)

SharedPreferences sp = getSharedPreferences("userinfo", 0)

SharedPreferences.Editor editor=sp.edit()

editor.putInt("locale", language)

editor.commit()

} catch (Exception e) {

e.printStackTrace()

}

}

工具/材料:以安卓手机为例。

1、首先在桌面上,点击“设置”图标。

2、然后在该界面中,点击“Additional settings”选项。

3、之后在该界面中,点击“Languages &input”选项。

4、接着在该界面中,点击“Languages”选项。

5、然后在该界面中,点击“中文(简体)”选项。

6、最后在该界面中,显示系统设置成功改成中文。

1 .在工程res文件下添加对应语种的values文件,ar:阿拉伯语, en:英语    zh_rCN: 简体中文

       截图如下:

2 .在功能清单文件中对要进行切换acitivity 进行配置添加

 android:configChanges="locale"

3 .对语言选择的处理

public class MainActivity extends Activity {

   Context context = MainActivity.this

   int languageId

   @Override

   public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState)

       read()//设置

       setContentView(R.layout.main)

       findViewById(R.id.btn_change).setOnClickListener(new OnClickListener() {

           @Override

           public void onClick(View v) {

               save()

           }

       })

   }

//保存

   private void save() {

       String[] languages = { "默认", "CN", "EN" }

       AlertDialog.Builder builder = new AlertDialog.Builder(context,

               android.R.style.Animation_Dialog)

       builder.setTitle("选择语言")

       final SharedPreferences languagePre = context.getSharedPreferences(

               "language_choice", context.MODE_PRIVATE)

       final int id = languagePre.getInt("id", 0)

       builder.setSingleChoiceItems(languages, id,

               new DialogInterface.OnClickListener() {

                   @Override

                   public void onClick(DialogInterface arg0, int index) {

                       switch (index) {

                       case 0:

                           // 系统默认语言

                           languageId = 0

                           break

                       case 1:

                           // 简体中文

                           languageId = 1

                           break

                       case 2:

                           // 英语

                           languageId = 2

                           break

                       case 3:

                           // 阿拉伯语

                           languageId = 3

                           break

                       default:

                           break

                       }

                       languagePre.edit().putInt("id", languageId).commit()

                   }

               })

       // 保存

       builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {

           @Override

           public void onClick(DialogInterface dialog, int which) {

               ((Activity) context).finish()

               Intent intent = new Intent()

               intent.setClass(context, MainActivity.class)

               context.startActivity(intent)

           }

       })

       builder.show()

   }

//读取

   private void read() {

       SharedPreferences languagePre = getSharedPreferences("language_choice",

               Context.MODE_PRIVATE)

       int id = languagePre.getInt("id", 0)

       Log.d("MainActivity", "langauge_id=" + id)

       Toast.makeText(context, "langauge_id=" + id, Toast.LENGTH_LONG).show()

       // 应用内配置语言

       Resources resources = getResources()// 获得res资源对象

       Configuration config = resources.getConfiguration()// 获得设置对象

       DisplayMetrics dm = resources.getDisplayMetrics()// 获得屏幕参数:主要是分辨率,像素等。

       switch (id) {

       case 0:

           config.locale = Locale.getDefault()// 系统默认语言

           break

       case 1:

           config.locale = Locale.SIMPLIFIED_CHINESE// 简体中文

           break

       case 2:

           config.locale = Locale.ENGLISH// 英文

           break

       default:

           config.locale = Locale.getDefault()

           break

       }

       resources.updateConfiguration(config, dm)

   }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11073669.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存