gpt4 book ai didi

java - android studio 无法设置特定的语言环境

转载 作者:太空狗 更新时间:2023-10-29 14:48:19 26 4
gpt4 key购买 nike

我无法在按下按钮时设置特定的语言环境。当按下特定语言的按钮时。我希望按钮重定向到主菜单页面,然后在那里呈现由五个按钮组成的基本结构。

我需要在模拟器中安装语言吗?它们不存在于模拟器中

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.selectlocale);

Button eng =(Button)findViewById(R.id.en);
eng.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Locale local=new Locale("en");
GlobalClass globalvariable=(GlobalClass)getApplicationContext();
globalvariable.setLocale(local);
Intent ints =new Intent(getApplication(),MainMenu.class);
startActivity(ints);
}
});

Button dar =(Button)findViewById(R.id.dr);
dar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Locale local=new Locale("fa");
GlobalClass globalvariable=(GlobalClass)getApplicationContext();
globalvariable.setLocale(local);
Intent ints =new Intent(getApplication(),MainMenu.class);
startActivity(ints);
}
});

Button pas =(Button)findViewById(R.id.ps);
pas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Locale local=new Locale("ps");
GlobalClass globalvariable=(GlobalClass)getApplicationContext();
globalvariable.setLocale(local);
Intent ints =new Intent(getApplication(),MainMenu.class);
startActivity(ints);
}
});
}

然后重定向到主菜单 Activity

   protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GlobalClass glb=(GlobalClass)getApplicationContext();
Locale loc =glb.getLocale();
Locale.setDefault(loc);
Configuration config =new Configuration();
config.locale=loc;
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.main_menu);
}

mainmenu.xml 页面

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.sepia2.myapplication.Main3Activity">


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link1"
android:id="@+id/link1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp"
android:layout_gravity="center_horizontal" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link2"
android:id="@+id/link2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link3"
android:id="@+id/link3"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link4"
android:id="@+id/link4"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Link5"
android:id="@+id/link5"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal" />

我的值(value)观-fa

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Darri HajjApp</string>
<string name="link1">darri link1</string>
<string name="link2">darri link2</string>
<string name="link3">darri link3</string>
<string name="link4">darri link4</string>
<string name="link5">darri link5</string>
</resources>

我的值(value)观-ps

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">pashto HajjApp</string>
<string name="link1">pashto link1</string>
<string name="link2">pashto link2</string>
<string name="link3">pashto link3</string>
<string name="link4">pashto link4</string>
<string name="link5">pashto link5</string>
</resources>

全局类

    public class GlobalClass extends Application {

private Locale _slocale;

public void setLocale(Locale loc)
{
_slocale=loc;
}

public Locale getLocale()
{
return _slocale;
}
}

最佳答案

这是我在 xml 文件中遗漏的内容(上面是完整的有效代码)

只是改变而不是

android:text="Link1"

应该是资源

 android:text="@string/link1"

并且不要忘记在 AndroidManifest 中添加 GlobalClass

  <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"

android:name="com.example.sepia2.myapplication.GlobalClass">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".settlocale" />
<activity android:name=".Main3Activity" />
<activity android:name=".MainMenu"></activity>


</application>

关于java - android studio 无法设置特定的语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37590415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com