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

时间:2016-06-02 11:22:02

标签: java android android-studio

我无法在按下按钮时设置特定区域设置。按下按钮时按下特定的lang。我希望按钮重定向到主菜单页面,然后呈现由五个按钮组成的基本结构。

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

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);
        }
    });
}

然后重定向到Mainmenu活动

   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" />

我的价值观-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>

我的价值观-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;
    }   
}

1 个答案:

答案 0 :(得分:0)

这是我在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>