我有一个Android应用程序,在values/strings.xml中有英文字符串.对于该文件中的每个字符串,我在values-ja/strings.xml中有一个条目,其中包含该字符串的日语翻译.如果我将模拟器,Nexus One或Nexus S设置为日语,则UI会显示日语文本.大多数时候.
有时,UI的某些部分将以英语显示,即使当前区域设置为ja-JP.例如,我在我的一个活动的onCreate()方法中编写了这个测试代码:
Log.e(TAG, "Default locale = '" + Locale.getDefault().toString() + "'");
Log.e(TAG, "My string = '" + getResources().getString(R.string.my_string) + "'");
Run Code Online (Sandbox Code Playgroud)
有时我会在LogCat中看到:
Default locale is 'ja_JP'
My string is '??'
Run Code Online (Sandbox Code Playgroud)
其他时候我会看到:
Default locale is 'ja_JP'
My string is 'English'
Run Code Online (Sandbox Code Playgroud)
有时通过旋转手机可以解决此问题.有时它会通过退出并重新启动应用程序来解决.有时只有一个屏幕的一部分是英文的.有时,通过代码从资源中提取的字符串会出现此问题,有时会出现仅由布局引用的字符串.我的应用程序中没有任何地方可以调用Locale.setDefault(),因此不会导致问题.
UPDATE
我找到了一种方法来纠正特定活动的问题.在该活动的onCreate()中:
Log.e(TAG, "getString: '" + getString(R.string.my_string) + "'");
Log.e(TAG, "getResources().getConfiguration(): '" +
getResources().getConfiguration().toString() + "'");
Log.e(TAG, "getResources().getDisplayMetrics(): '" +
getResources().getDisplayMetrics().toString() + "'");
Log.e(TAG, "Setting configuration to getConfiguration()");
getResources().updateConfiguration(getResources().getConfiguration(),
getResources().getDisplayMetrics());
Log.e(TAG, "getString: '" + getString(R.string.my_string) + "'");
Log.e(TAG, "getResources().getConfiguration(): …Run Code Online (Sandbox Code Playgroud) 在为Android编程时,有时您必须使用静态方法.但是当您尝试以静态方法访问资源时,getString(R.string.text)您将收到错误.使其静止不起作用.
有没有人知道这个好方法?Android中的资源文件对于使用不同语言创建内容或更改文本非常有用.
可能重复:
动态资源加载Android
在Android中,我可以从资源中加载一个字符串String s = getString(R.string.keyName).但是我的数据库中有一个类别列表,每个类别都有一个名称.如何获取该类别名称,然后根据它添加适当的字符串资源,以便它可以用于本地化?
基本上,我需要有keyName动力; 我自己的String变量.这可能吗?谢谢.
我有一个从外部实体收到的字节数组.它是固定的大小.这些字节包含一个unicode字符串,其中0个值用于填充缓冲区的其余部分:
所以字节可能是:
H \0 E \0 L \0 L \0 \0 \0 \0 \0 \0 ... etc
Run Code Online (Sandbox Code Playgroud)
我正在获取缓冲区并将其转换为字符串,如下所示:
byte[] buffer = new byte[buffSize];
m_dataStream.Read(buffer, 0, buffSize);
String cmd = System.Text.Encoding.Unicode.GetString(buffer);
Run Code Online (Sandbox Code Playgroud)
我得到的是一个看起来像这样的字符串:
"HELLO\0\0\0\0\0\0\0\0..."
Run Code Online (Sandbox Code Playgroud)
我如何告诉GetString在第一个Unicode null处终止字符串(即我只是回到"HELLO")?
感谢您的任何意见.
我试图填补Spinner与一些价值Strings的string.xml.我想这样做:
final List<String> list = new ArrayList<String>();
for (int i = 0; i < 86; i++) {
String resource = "R.string." + ntpServers[i][0];
list.add(getResources().getString(resource));
}
Run Code Online (Sandbox Code Playgroud)
我有类似下一个的字符串.
<string name="AT">Austria</string>
<string name="BY">Belarus</string>
<string name="BE">Belgium</string>
Run Code Online (Sandbox Code Playgroud)
而从地理编码,我得到像AT,BY,等存储这些ntpServers阵列中的国家代码.我想用相应的字符串中保存的名称填充微调器,但我不能这样做,getResources().getString(resource)因为resource它String不是一个int.
我怎么解决这个问题?
谢谢你的建议.
PD:
我得到了下一个序列:
例如:
geocoder.getCountry() = ES --> ntpServers[36][0] = ES --> R.string.ES = """Spain"""
Run Code Online (Sandbox Code Playgroud)
我想在微调器中使用哪个字符串.
_ __ _ __ _ __ _ _ _ _ _ _ _ _ …
我该如何解决这个错误.底部的所有三个字符串都会出现以下错误"对于Apps类型,方法getString(int)未定义".请帮忙,我是这样的菜鸟.
package com.actionbarsherlock.sample.fragments;
import android.content.Context;
import android.content.res.Resources;
public final class Apps {
/**
* Our data, part 1.
*/
public static final String[] TITLES =
{
"title1",
"title2",
"title3"
};
/**
* Our data, part 2.
*/
public static final String[] DIALOGUE = {
getString(R.string.text1),
getString(R.string.string2),
getString(R.string.string3)
};
}
Run Code Online (Sandbox Code Playgroud) 我使用它getString()从string.xml成为String.在我的班级(非活动)不起作用:
如何获取此类的String?
public class myClass{
public String[] myInfo(String ID) {
String myString = getRessources().getString(R.string.myString);
};
}
Run Code Online (Sandbox Code Playgroud) @Override
public void onPause()
{
super.onPause();
save(notes.itemSelected);
}
@Override
public void onResume()
{
super.onResume();
notes.itemSelected.clear();
notes.itemSelected = load();
}
@Override
public void onRestart()
{
super.onRestart();
notes.itemSelected.clear();
notes.itemSelected = load();
}
private void save(final ArrayList<String> isChecked) {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
for(Integer i = 0; i < isChecked.size(); i++)
{
editor.putString(i.toString(), isChecked.get(i));
}
editor.commit();
}
private ArrayList<String> load() {
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
ArrayList<String> reChecked = new ArrayList<String>();
for(Integer i = 0; i < notes.getCount(); i++)
{ …Run Code Online (Sandbox Code Playgroud) 我知道Cursor有get String, Int等方法,但不是有些东西
mycursor.GetObject(index)
Run Code Online (Sandbox Code Playgroud)
我想创建一个返回一个对象的动态方法,我只抛出它.
或者可以
mycursor.GetString(index)用于任何类型? String,Float,Double,Long,Short, Blob,Int,等等
我可以使用例如a Float或a Int或任何类型并投射它?
例如
(Blob) newblob=mycursor.GetString(i_return_BloB);
(Int) newint=mycursor.GetString(i_return_Int);
(Float) newfloat=mycursor.GetString(i_return_Float);
(Double) newdouble=mycursor.GetString(i_return_Double);
(Long) newlong=mycursor.GetString(i_return_long);
(Short) newshort=mycursor.GetString(i_return_short);
Run Code Online (Sandbox Code Playgroud)
会有用吗?我可以mycursor.GetString()用于任何类型吗?
在 MonoGame 中,如何以字符串的形式读取按下了哪个键盘键?
我试过String pressedKey = Keyboard.GetState().ToString();,但它给了我“Microsoft.Xna.Framework.Input.KeyboardState”。