如何加载与微调器选择相关的文本字符串
How to load a string of text relating to a spinner selection
我有一个显示数组列表的微调器,我想要做的是让 selected 选项在 window 下方显示更详细的 selection数组。
我这样做的原因是我想构建一个角色创建工具,我希望人们能够从选项列表中进行选择,然后根据选项 select 提出描述select离子意味着什么。
到目前为止,我的数组在 strings.xml 中,它是:
<string-array name="humanBackgroundData">
<item>Cultist</item>
<item>Freed Slave</item>
<item>Gangster</item>
<item>Techno-Reaper</item>
<item>Tribal</item>
<item>Wanderer</item>
<item>Dwelled in the Dwelling</item>
<item>Feral Child</item>
<item>Welsh/Scottish Descendant</item>
</string-array>
我的 Spinner 在 activity.xml:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinB"
android:entries="@array/humanBackgroundData"
android:paddingBottom="20dp"
>
我一直在寻找一种方法来 link 将 selected 选项添加到另一个字符串,然后可以显示该 selection 的其他数据。我想这样做是因为微调器中有大量数据看起来很糟糕。我已经看到了对微调器进行编号的方法,因此第一个 selection 为 1,第二个为 2,所以我相信我应该能够将其用作调用字符串的参考,但我一直在使用android studio 大约两周,我找不到任何其他关于这样做的帖子。有什么建议吗?
感谢您的宝贵时间:)
更新
我现在已经部分找到了解决方案,如果我将它包含在 activity 中,我现在可以让 TextView 引用该字符串,但是我想从 strings.xml 中提取它,所以它保持事情有点整洁,让我不再重复自己的话。我在下面 link 编辑了我的代码。如果我用 "text1" 替换 "textnone" 它可以工作,所以我认为这只是我 link 编辑字符串数组的方式的问题。
<string-array name="humanBackgroundData">
<item>Cultist</item>
<item>Freed Slave</item>
<item>Gangster</item>
<item>Techno-Reaper</item>
<item>Tribal</item>
<item>Wanderer</item>
<item>Dwelled in the Dwelling</item>
<item>Feral Child</item>
<item>Welsh/Scottish Descendant</item>
</string-array>
package com.studios.grimvoodoo.spicedcharacterbuilder;
import android.content.res.Resources;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class HumanActivity extends Activity {
private static Button proceed;
Resources res = getResources();
String[] text1 = res.getStringArray(R.array.ghulBackgroundData);
String[] textnone = {"human", "ghul", "postgen",
"gnome", "pixie", "giant", "vampire"};
String[] text2 = {"standard", "undead", "super mutant", "wee man", "flying wee man", "big and hungry", "dead and loving it"};
Spinner spinner1;
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_human);
textView1 = (TextView) findViewById(R.id.text1);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(HumanActivity.this,
android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 =
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String s1 = String.valueOf(text2[position]);
textView1.setText(s1);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.studios.grimvoodoo.spicedcharacterbuilder.HumanActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<string-array name="humanBackgroundData">
<item>Cultist</item>
<item>Freed Slave</item>
<item>Gangster</item>
<item>Techno-Reaper</item>
<item>Tribal</item>
<item>Wanderer</item>
<item>Dwelled in the Dwelling</item>
<item>Feral Child</item>
<item>Welsh/Scottish Descendant</item>
</string-array>
如果我没理解错,你应该在布局中将额外的 TextView 放在 Spinner 下方。然后创建带有附加信息(信息数组)的单独字符串数组,其大小与现有字符串数组相同。
然后在微调器的 onItemSelected(AdapterView parent, View view, int pos, long id) 方法中,获取所选微调器元素 (pos) 的位置,并使用 infos 数组中的相应元素(具有索引 pos 的元素)更新 TextView。
我有一个显示数组列表的微调器,我想要做的是让 selected 选项在 window 下方显示更详细的 selection数组。
我这样做的原因是我想构建一个角色创建工具,我希望人们能够从选项列表中进行选择,然后根据选项 select 提出描述select离子意味着什么。
到目前为止,我的数组在 strings.xml 中,它是:
<string-array name="humanBackgroundData">
<item>Cultist</item>
<item>Freed Slave</item>
<item>Gangster</item>
<item>Techno-Reaper</item>
<item>Tribal</item>
<item>Wanderer</item>
<item>Dwelled in the Dwelling</item>
<item>Feral Child</item>
<item>Welsh/Scottish Descendant</item>
</string-array>
我的 Spinner 在 activity.xml:
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinB"
android:entries="@array/humanBackgroundData"
android:paddingBottom="20dp"
>
我一直在寻找一种方法来 link 将 selected 选项添加到另一个字符串,然后可以显示该 selection 的其他数据。我想这样做是因为微调器中有大量数据看起来很糟糕。我已经看到了对微调器进行编号的方法,因此第一个 selection 为 1,第二个为 2,所以我相信我应该能够将其用作调用字符串的参考,但我一直在使用android studio 大约两周,我找不到任何其他关于这样做的帖子。有什么建议吗?
感谢您的宝贵时间:)
更新
我现在已经部分找到了解决方案,如果我将它包含在 activity 中,我现在可以让 TextView 引用该字符串,但是我想从 strings.xml 中提取它,所以它保持事情有点整洁,让我不再重复自己的话。我在下面 link 编辑了我的代码。如果我用 "text1" 替换 "textnone" 它可以工作,所以我认为这只是我 link 编辑字符串数组的方式的问题。
<string-array name="humanBackgroundData">
<item>Cultist</item>
<item>Freed Slave</item>
<item>Gangster</item>
<item>Techno-Reaper</item>
<item>Tribal</item>
<item>Wanderer</item>
<item>Dwelled in the Dwelling</item>
<item>Feral Child</item>
<item>Welsh/Scottish Descendant</item>
</string-array>
package com.studios.grimvoodoo.spicedcharacterbuilder;
import android.content.res.Resources;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class HumanActivity extends Activity {
private static Button proceed;
Resources res = getResources();
String[] text1 = res.getStringArray(R.array.ghulBackgroundData);
String[] textnone = {"human", "ghul", "postgen",
"gnome", "pixie", "giant", "vampire"};
String[] text2 = {"standard", "undead", "super mutant", "wee man", "flying wee man", "big and hungry", "dead and loving it"};
Spinner spinner1;
TextView textView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_human);
textView1 = (TextView) findViewById(R.id.text1);
spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(HumanActivity.this,
android.R.layout.simple_spinner_item, text1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(onItemSelectedListener1);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 =
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String s1 = String.valueOf(text2[position]);
textView1.setText(s1);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.studios.grimvoodoo.spicedcharacterbuilder.HumanActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text0"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<string-array name="humanBackgroundData">
<item>Cultist</item>
<item>Freed Slave</item>
<item>Gangster</item>
<item>Techno-Reaper</item>
<item>Tribal</item>
<item>Wanderer</item>
<item>Dwelled in the Dwelling</item>
<item>Feral Child</item>
<item>Welsh/Scottish Descendant</item>
</string-array>
如果我没理解错,你应该在布局中将额外的 TextView 放在 Spinner 下方。然后创建带有附加信息(信息数组)的单独字符串数组,其大小与现有字符串数组相同。
然后在微调器的 onItemSelected(AdapterView parent, View view, int pos, long id) 方法中,获取所选微调器元素 (pos) 的位置,并使用 infos 数组中的相应元素(具有索引 pos 的元素)更新 TextView。