使用字符串文件在android应用程序中更改语言 [英] Change language in android application using string file

查看:63
本文介绍了使用字符串文件在android应用程序中更改语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,实际上是在开发游戏应用程序,我想添加一个可以更改游戏语言的功能.

I'm new in java and actually developing a game app and I wanted add a feature which could change languages in the game.

我已经制作了2个strings.xml.一个是默认名称(英语),另一个是翻译版本(文件)

I already made 2 strings.xml. One is the default (English), the other one is the translated version (File)

这是我的代码

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LanguageActivity extends Activity {
  private static Button button_fil;
  private static Button button_eng;

  public void onButtonClickListener() {
    button_fil = (Button) findViewById(R.id.btnFilipino);
    button_fil.setOnClickListener(
      new View.OnClickListener() {@
        Override
        public void onClick(View v) {
          Toast.makeText(LanguageActivity.this, "Filipino Language", Toast.LENGTH_SHORT).show();
        }
      }
    );

    button_eng = (Button) findViewById(R.id.btnEnglish);
    button_eng.setOnClickListener(
      new View.OnClickListener() {@
        Override
        public void onClick(View v) {
          Toast.makeText(LanguageActivity.this, "English Language", Toast.LENGTH_SHORT).show();
        }
      }
    );

  }


  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.language);
    onButtonClickListener();

  }

非常感谢!

推荐答案

请尝试以下示例.也许会对您有帮助. 在这里,我使用了微调器来选择语言.

Try this example please. Maybe it will help you. Here i used a spinner for selecting language.

在活动中

import java.util.Locale;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class AndroidLocalize extends Activity {
    Spinner spinnerctrl;
    Button btn;
    Locale myLocale;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        spinnerctrl = (Spinner) findViewById(R.id.spinner1);
        spinnerctrl.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View view,
                    int pos, long id) {

                if (pos == 1) {

                    Toast.makeText(parent.getContext(),
                            "You have selected Tamil", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("ta");
                } else if (pos == 2) {

                    Toast.makeText(parent.getContext(),
                            "You have selected Hindi", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("hi");
                } else if (pos == 3) {

                    Toast.makeText(parent.getContext(),
                            "You have selected English", Toast.LENGTH_SHORT)
                            .show();
                    setLocale("en");
                }

            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }

        });
    }

    public void setLocale(String lang) {

        myLocale = new Locale(lang);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);
        Intent refresh = new Intent(this, AndroidLocalize.class);
        startActivity(refresh);
    }
}

以您的XML格式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/greet"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/greet"
        android:textSize="25sp" android:gravity="center" android:paddingTop="25sp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/langselection"
        android:textAppearance="?android:attr/textAppearanceMedium" android:gravity="center" android:paddingTop="25sp"/>


 <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/languages"
       android:gravity="center" android:paddingTop="25sp" />

</LinearLayout>

并在您的资源中创建文件夹,例如

and create folders in your res like

然后为您的语言添加strings.xml 像

then add strings.xml for your language like

<resources>

    <string name="app_name">Androidlocalization</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">बधाई सचिन !!</string>
    <string name="langselection">जिस भाषा में आप सचिन को नमस्कार करना चाहते हैं का चयन करें!!!!</string>
    <string name="chooselang">Choose the language</string>
      <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
    </string-array>

</resources>

也请更新您的清单,希望能解决您的问题.

please update your manifest also , i hope that will resolve your problem..

像这样更新.

 <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".AndroidLocalize"
                android:label="@string/title_activity_android_localize" 
android:configChanges="locale|orientation|keyboardHidden" 
android:noHistory="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

这篇关于使用字符串文件在android应用程序中更改语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆