Java Android-遍历strings.xml文件
我想知道是否还有循环通过Java Android-遍历strings.xml文件,java,android,xml,loops,Java,Android,Xml,Loops,我想知道是否还有循环通过strings.xml文件的方法 假设我有以下格式: 修改密码 更改URL 密码 http://xxx:8080 testPhoneAccount 调试设置 每次启动时重新加载\u数据\u 在首次启动时重新加载数据 现在让我们假设我有这个: private HashMap hashmapStringValues=new HashMap(); 有没有办法只在xml文件的第二部分进行迭代?也许可以用一个像这样的标记来包装该部分,然后遍历它 public void ini
strings.xml
文件的方法
假设我有以下格式:
修改密码
更改URL
密码
http://xxx:8080
testPhoneAccount
调试设置
每次启动时重新加载\u数据\u
在首次启动时重新加载数据
现在让我们假设我有这个:
private HashMap hashmapStringValues=new HashMap();
有没有办法只在xml文件的第二部分进行迭代?也许可以用一个像
这样的标记来包装该部分,然后遍历它
public void initHashMap(){
for(int i=0;i<??;i++)//这里我只需要在xml文件的第二部分循环
{
String nameOfTag=?//这里我得到标记的名称
int value=R.string.nameOfTag//这里我得到标记的相关值
this.hashmapStringValues.put(nameOfTag,value);
}
}
这是不可能的,因为基本java本身没有检测到注释
您只能像这样选择字符串
getResources().getString(R.string.app_name)
不可以,但您可以在资源/值中创建另一个xml文件,该文件包含以下内容:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="array">
<item>ID_1|Asdf</item>
<item>ID_2|I do not like to think</item>
<item>ID_3|Whatever</item>
</string-array>
</resources>
如果您查看生成的android.R java文件,它会告诉您如何通过反射实现这一点:
Field fields[] = R.string.class.getFields();
for (Field field : fields) {
Log.d("appname", field.getName() + ":" + getResources().getIdentifier(field.getName(), "string", this.getPackageName()));
}
注意,我不会经常这样做,但一次应用程序加载就可以了。公共类MainActivity扩展了AppCompatActivity{
public class MainActivity extends AppCompatActivity{
// one way to iterate over the arrays in the strings.xml file
// is to name them all here......
String[] categories = {"millis","size_uk","size_us","size_jp"};
ArrayList<Spinner> spinners = new ArrayList<>();
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// iterate over the views in the layout and find the spinners
// need to iterate over spinners separately
// because need to identify them individually
// in order to set their selection in the event handlers
// of the other spinners
GridLayout gridLayout = findViewById(R.id.GridLayout1);
for (int i = 0; i < gridLayout.getChildCount(); i++) {
View view = gridLayout.getChildAt(i);
// if the view is a spinner.....
if (view instanceof Spinner){
spinners.add((Spinner)view);
}
}
// it is an array of arrays
// iterate over the arrays and fill each spinner
// they used to be called strange objects
// plus attach an event to each spinner
for (int i = 0; i < categories.length; i++){
Spinner spinner = spinners.get(i);
// identify the array from it's name in the resource file
int id = this.getResources().getIdentifier(categories[i], "array",
this.getPackageName());
// create an adapter from said array
adapter = ArrayAdapter.createFromResource(this, id,
android.R.layout.simple_spinner_item);
//put the contents of the array in the spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//set listener event for each spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// set all spinners to the same index
for(int k = 0; k < 4; k++)
spinners.get(k).setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
});
}
}
//在strings.xml文件中迭代数组的一种方法
//就是在这里给他们命名。。。。。。
String[]categories={“millis”、“size_uk”、“size_us”、“size_jp”};
ArrayList微调器=新的ArrayList();
阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//迭代布局中的视图并找到微调器
//需要分别迭代微调器
//因为我们需要单独识别它们
//以便在事件处理程序中设置它们的选择
//其他纺纱厂的
GridLayout GridLayout=findviewbyd(R.id.GridLayout1);
对于(int i=0;i
}为什么。。。使用数组。。。或者使用普通xml进行设置…因为我需要在javascript文档中传递每个值,每个值在HTML表单中都有一个与名称标记对应的id,所以我不能使用类似的内容:Key 1 Key 2 Key 3 Key 4 Key 5 Key 6
首先,如果您将任何xml放在原始文件夹以外的文件夹中,数据将不会保留为xml。aapt将生成一个更优化的压缩blob,您无法像解析xmlkey | value
那样解析它。。。使用设置创建xml(将其放入资产),并使用AssetManager.openXmlResourceParser(…)
对其进行解析…非常有用:R.string.class.getFields()
,谢谢!没问题,这让我很高兴,在我生命中的某个时刻我是有用的:)
public class MainActivity extends AppCompatActivity{
// one way to iterate over the arrays in the strings.xml file
// is to name them all here......
String[] categories = {"millis","size_uk","size_us","size_jp"};
ArrayList<Spinner> spinners = new ArrayList<>();
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// iterate over the views in the layout and find the spinners
// need to iterate over spinners separately
// because need to identify them individually
// in order to set their selection in the event handlers
// of the other spinners
GridLayout gridLayout = findViewById(R.id.GridLayout1);
for (int i = 0; i < gridLayout.getChildCount(); i++) {
View view = gridLayout.getChildAt(i);
// if the view is a spinner.....
if (view instanceof Spinner){
spinners.add((Spinner)view);
}
}
// it is an array of arrays
// iterate over the arrays and fill each spinner
// they used to be called strange objects
// plus attach an event to each spinner
for (int i = 0; i < categories.length; i++){
Spinner spinner = spinners.get(i);
// identify the array from it's name in the resource file
int id = this.getResources().getIdentifier(categories[i], "array",
this.getPackageName());
// create an adapter from said array
adapter = ArrayAdapter.createFromResource(this, id,
android.R.layout.simple_spinner_item);
//put the contents of the array in the spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//set listener event for each spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// set all spinners to the same index
for(int k = 0; k < 4; k++)
spinners.get(k).setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
// do nothing
}
});
}
}