【问题标题】:Change text after changing locale更改语言环境后更改文本
【发布时间】:2014-02-17 23:29:15
【问题描述】:

好的,伙计们。我正在做一个项目,我在操作栏中放了一个按钮,可以将保加利亚语翻译成英语,反之亦然。我已经在语言环境中进行了更改,但我的问题是,在我单击按钮后,应用程序中的语言会发生变化,但操作栏中按钮上的文本不会。我创建了两个文件夹 values-bg 和 values-en,我为按钮使用的字符串在两个 string.xmls 中都声明了,但文本根本没有改变。

这是代码:

import java.util.Locale;

import com.pushbots.push.Pushbots;

import android.net.Uri;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    Button lBtn, sBtn, cBtn, aBtn;
    TextView hLink;

    private String loc = "loc";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar bar = getActionBar();
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#118cec")));

        setContentView(R.layout.activity_main);
        this.lBtn = (Button) findViewById(R.id.button1);
        lBtn.setOnClickListener(this);
        this.sBtn = (Button) findViewById(R.id.button2);
        sBtn.setOnClickListener(this);
        this.cBtn = (Button) findViewById(R.id.button3);
        cBtn.setOnClickListener(this);
        this.aBtn = (Button) findViewById(R.id.button4);
        aBtn.setOnClickListener(this);
      this.hLink = (TextView) findViewById(R.id.hlink);
       hLink.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        if (v.getId() == lBtn.getId()) {

            startActivity(new Intent(this, lect_list.class));
            Log.d(loc, "pressed");

        } else if (v.getId() == sBtn.getId()) {
            Toast t = Toast.makeText(this, "Text",
                    Toast.LENGTH_LONG);
            t.show();
        } else if (v.getId() == cBtn.getId()) {

            Toast t = Toast.makeText(this,
                    "Text",
                    Toast.LENGTH_LONG);
            t.show();
        } else if (v.getId() == aBtn.getId()) {

            startActivity(new Intent(this, dlect.class));
            Log.d(loc, "pressed");
        }
        else if (v.getId() == hLink.getId()) {

            Toast t = Toast.makeText(this, "Text",
                    Toast.LENGTH_LONG);
            t.show();


            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
        }

    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        if (item.getItemId() == R.id.trans) {

            Toast t = Toast.makeText(this, "Translation", Toast.LENGTH_LONG);
            t.show();

            Locale mLocale = new Locale("bg");
            Locale.setDefault(mLocale);
            Configuration config = getBaseContext().getResources()
                    .getConfiguration();
            if (!config.locale.equals(mLocale)) {
                config.locale = mLocale;
                getBaseContext().getResources().updateConfiguration(config,
                        null);
                setContentView(R.layout.activity_main);
                this.lBtn = (Button) findViewById(R.id.button1);
                lBtn.setOnClickListener(this);
                this.sBtn = (Button) findViewById(R.id.button2);
                sBtn.setOnClickListener(this);
                this.cBtn = (Button) findViewById(R.id.button3);
                cBtn.setOnClickListener(this);
                this.aBtn = (Button) findViewById(R.id.button4);
                aBtn.setOnClickListener(this);

            } else {
                mLocale = new Locale("en");
                config.locale = mLocale;
                getBaseContext().getResources().updateConfiguration(config,
                        null);
                setContentView(R.layout.activity_main);


                this.lBtn = (Button) findViewById(R.id.button1);
                lBtn.setOnClickListener(this);
                this.sBtn = (Button) findViewById(R.id.button2);
                sBtn.setOnClickListener(this);
                this.cBtn = (Button) findViewById(R.id.button3);
                cBtn.setOnClickListener(this);
                this.aBtn = (Button) findViewById(R.id.button4);
                aBtn.setOnClickListener(this);

            }
        }

        return super.onOptionsItemSelected(item);
    }


}

是否有可能以某种方式再膨胀一次菜单。更改语言环境后,我设置了新活动,但没有设置新菜单。我可以打电话给 getMenuInflater().inflate(R.menu.main, menu);以某种方式在我的 If 块中?

【问题讨论】:

  • 可能有很多东西。请发布您的代码。

标签: android android-actionbar locale


【解决方案1】:

在更改区域设置后调用 invalidateOptionsMenu()... 或者只是重新启动您的应用程序 :)

【讨论】:

  • 非常感谢。这就是我一直在寻找的。这可能很容易,但我m new in android and Im 很高兴你帮助了我:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
相关资源
最近更新 更多