【问题标题】:TextView not changing when changing Locale of app更改应用程序的区域设置时 TextView 未更改
【发布时间】:2018-04-12 16:18:55
【问题描述】:

onOptionsItemSelected 中选择选项时,我正在尝试更改应用程序的locale

当我尝试这样做时,语言会在同一个菜单中成功更改。

但是,布局中的TextView 中的text 不会改变。

感谢任何帮助。

我的 MainActivity 是:

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Locale myLocale;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        textView = (TextView) findViewById(R.id.textView1);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_english) {
            Toast.makeText(getApplicationContext(),
                    "You have selected English", Toast.LENGTH_SHORT)
                    .show();
            setLocale("en");
            return true;
        } else if (id == R.id.action_nepal) {
            Toast.makeText(getApplicationContext(),
                    "You have selected Nepali", Toast.LENGTH_SHORT)
                    .show();
            setLocale("ne");
            return true;
        }


        return super.onOptionsItemSelected(item);
    }


    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);
        getBaseContext().getResources().updateConfiguration(conf,
                getBaseContext().getResources().getDisplayMetrics());
        invalidateOptionsMenu();
        Intent refresh = new Intent(this, MainActivity.class);
        startActivity(refresh);

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // refresh your views here
        super.onConfigurationChanged(newConfig);
    }


}

我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.languagesupport">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:configChanges="locale"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

还有我的两个字符串文件:

英语:

<resources>
    <string name="app_name">languageChange</string>
    <string name="sample_text">Hello World</string>
    <string name="action_settings">Settings</string>
    <string name="english">English</string>
    <string name="nepal">Nepal</string>
</resources>

尼泊尔人:

<resources>
    <string name="app_name">भीमदत्त नगरपालिका</string>
    <string name="action_settings">गृहपृष्ठ</string>
    <string name="sample_text">" विकास क्षेत्रमा पर्ने महाकाली अञ्चल), कञ्चनपुर जिल्लाको स"</string>
    <string name="english">अंग्रेजी</string>
    <string name="nepal">नेपाल</string>
</resources>

【问题讨论】:

    标签: android locale


    【解决方案1】:

    重新开始你的活动后试试这个用法finish();

    Intent refresh = new Intent(this, MainActivity.class);
    startActivity(refresh);
    finish();
    

    【讨论】:

    • 不..还是一样
    • @An08NY 您在哪个设备上测试
    • Nexus 6P 安卓模拟器
    • @An08NY 在真实设备中尝试文本案例
    • 对不起..又是同样的事情。
    【解决方案2】:

    有两种不同的方法:

    1.没有 Activity 重启:

    • 在清单中的&lt;activity&gt; 中设置android:configChanges="locale"

    • 如下更改您的 setLocale:

          private void setLocale(String lang) {
              Locale myLocale = new Locale(lang);
              Resources res = getResources();
              DisplayMetrics dm = res.getDisplayMetrics();
              Configuration conf = res.getConfiguration();
              conf.locale = myLocale;
              res.updateConfiguration(conf, dm);
              getBaseContext().getResources().updateConfiguration(conf, getBaseContext().getResources().getDisplayMetrics());
              invalidateOptionsMenu();
              onConfigurationChanged(conf);//Add this line
          }
      
    • 覆盖onConfigurationChanged():

          @Override
          public void onConfigurationChanged(final Configuration newConfig) {
              super.onConfigurationChanged(newConfig);
              textView.setText(<your-text>);
              //Any other UI text to change
          }
      

    这将确保您的区域设置正确更改。

    2。重新创建活动:

    代替

    Intent refresh = new Intent(this, MainActivity.class);
    startActivity(refresh);
    finish();
    

    打电话

    recreate();

    这将导致使用新实例重新创建此 Activity。

    【讨论】:

    • 这是第一个实际描述如何直接应用语言更改的答案,而不是切换到另一个活动/重新加载活动活动,所以谢谢!关于代码的评论: 1. 我在Fragment 中使用微调器来更改语言。当我尝试切换到另一个片段时,调用 getActivity.recreate() 会导致某种无限循环(一遍又一遍地调用 onCreateView())。 2. 在Fragment 中使用res 而不是getBaseContext().getResources() 似乎完全没问题,然后只需调用一次updateConfiguration()
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 2011-01-16
    • 2019-03-22
    相关资源
    最近更新 更多