lizongbo at 618119.com 工作,生活,Android,前端,Linode,Ubuntu,nginx,java,apache,tomcat,Resin,mina,Hessian,XMPP,RPC

2013年02月25日

Android 手动切换多国语言的完整例子

Filed under: Android — 标签:, — lizongbo @ 21:04

通过google搜索Android多国语言或Locale找到的都不是完整例子,很多代码并不靠谱。

于是摸索写了个完整例子,实现多国语言手动切换并重新以当前语言启动当前界面的效果。

0.首先使用向导创建默认的工程。

 

1.然后在res目录下创建values-zh-rCN存放中文语言资源文件,values目录下默认存放英文语言资源。

英文版的strings.xml内容为:

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<string name=”app_name”>localedemo</string>
<string name=”hello_world”>Hello world!</string>
<string name=”menu_settings”>Settings</string>

</resources>

中文版的strings.xml内容为:

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<string name=”app_name”>多国语言手工切换的例子</string>
<string name=”hello_world”>你好 世界!</string>
<string name=”menu_settings”>设置</string>

</resources>

values目录下创建:strings_locale_set.xml

内容为:

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<string name=”locale_zh”>中文</string>
<string name=”locale_en”>English</string>

</resources>

主界面代码为:LocaleMainActivity.java

package com.lizongbo.android.example.localedemo;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class LocaleMainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(“LocaleMainActivity”, “onCreate|”+(savedInstanceState!=null?savedInstanceState.keySet():savedInstanceState));
this.setTitle(R.string.app_name);
setContentView(R.layout.activity_locale_main);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(this.getBaseContext(), “修改了设置”, 1000).show();
}

@Override
protected void onRestart() {
super.onRestart();
Log.i(“LocaleMainActivity”, “onRestart”);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_locale_main, menu);
//添加设置为中文语言的菜单
MenuItem menu_locale_zh = menu.add(R.string.locale_zh);
//menu_locale_zh.sett
menu_locale_zh
.setOnMenuItemClickListener(new MenuItemLocaleSetOnMenuItemClickListener(
“zh_CN”, this));
//添加设置为英文语言的菜单
MenuItem menu_locale_en = menu.add(R.string.locale_en);
menu_locale_en
.setOnMenuItemClickListener(new MenuItemLocaleSetOnMenuItemClickListener(
“en”, this));
return true;
}

}

 

点击菜单实现语言切换及重启当前界面的代码:MenuItemLocaleSetOnMenuItemClickListener.java

package com.lizongbo.android.example.localedemo;

import java.util.Locale;

import android.app.*;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Toast;

public class MenuItemLocaleSetOnMenuItemClickListener implements
OnMenuItemClickListener {
private Locale locale = Locale.SIMPLIFIED_CHINESE;
private Activity activity = null;

public MenuItemLocaleSetOnMenuItemClickListener(String locale,
Activity activity) {
this.activity = activity;
Locale locales[] = Locale.getAvailableLocales();
for (Locale l : locales) {
if (l.toString().equalsIgnoreCase(locale)) {
this.locale = l;
}
}
}

@Override
public boolean onMenuItemClick(MenuItem item) {
if(Locale.getDefault().equals(locale)){
Toast.makeText(activity, “已经是”+locale+”,无需切换”, 1000).show();
return true;
}
Locale.setDefault(locale);//设置选定的语言
Configuration config = new Configuration();
config.locale = locale;
if (activity != null) {
Toast.makeText(activity, “”+locale, 1000).show();
//更新设置
activity.getResources().updateConfiguration(config,
activity.getResources().getDisplayMetrics());
activity.finish();
//重启当前界面
activity.startActivity(activity.getIntent());

//@toto 为了确保下次进入程序记住当前选择的语言,可以使用SharedPreferences 来记住设置。

SharedPreferences sharedPreferences=activity.getSharedPreferences(“com.lizongbo”, Context.MODE_PRIVATE);
sharedPreferences.edit().putString(“locale”, locale.toString()).commit();
}
return true;
}

}

 

 

 

 

 

 

2013年02月22日

Exception in thread “LWJGL Application” com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: OpenGL is not supported by the video driver.的解决办法

Filed under: Android,Java — 标签:, , , — lizongbo @ 11:25

按照网上的教程,下载最新的libgdx库,

解压:libgdx-nightly-latest.zip.然后运行里面的工程生成工具。

D:\Java\libgdx-nightly-latest>java -jar gdx-setup-ui.jar

导入生成的工程,结果运行出错:

Exception in thread “LWJGL Application” com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: OpenGL is not supported by the video driver.
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:113)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: OpenGL is not supported by the video driver.
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.createDisplayPixelFormat(LwjglGraphics.java:219)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setupDisplay(LwjglGraphics.java:181)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:122)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:110)
Caused by: org.lwjgl.LWJGLException: Pixel format not accelerated
at org.lwjgl.opengl.WindowsPeerInfo.nChoosePixelFormat(Native Method)
at org.lwjgl.opengl.WindowsPeerInfo.choosePixelFormat(WindowsPeerInfo.java:52)
at org.lwjgl.opengl.WindowsDisplay.createWindow(WindowsDisplay.java:229)
at org.lwjgl.opengl.Display.createWindow(Display.java:303)
at org.lwjgl.opengl.Display.create(Display.java:845)
at org.lwjgl.opengl.Display.create(Display.java:754)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.createDisplayPixelFormat(LwjglGraphics.java:216)
… 3 more

遇到这个错误,解决办法很简单,到nvidia官方网站下载对应的opengl驱动即可:

https://developer.nvidia.com/opengl-driver

http://developer.download.nvidia.com/devzone/devcenter/gamegraphics/files/opengl/4.3/306.63_desktop_win8_winvista_win7_64bit_international.exe

网上相关文章和讨论只说了显卡在十年前就支持Opengl了,却没提示用户该检查一下是否安装了显卡的OpenGL驱动。

参考:  http://code.google.com/p/libgdx/issues/detail?id=882

http://stackoverflow.com/questions/10972688/libgdx-exception-in-thread-lwjgl-application

http://code.google.com/p/libgdx/wiki/ProjectSetup

最新的非稳定包的下载地址:

http://libgdx.badlogicgames.com/nightlies/libgdx-nightly-latest.zip

 

 

Powered by WordPress