繁体   English   中英

如何加载与微调框选择有关的文本字符串

[英]How to load a string of text relating to a spinner selection

我有一个微调器,用于显示数组中的列表,我想做的是让选定的选项在数组下方的窗口中显示更详细的选择。

我这样做的原因是我想构建一个角色创建工具,并且希望人们能够从选项列表中进行选择,然后根据他们选择的选项来提供对该选择含义的描述。

到目前为止,我在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>

而我的微调器位于activity.xml中:

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinB"
    android:entries="@array/humanBackgroundData"
    android:paddingBottom="20dp"
    >

我一直在寻找一种将所选选项链接到另一个字符串的方法,该字符串然后可以显示该选择的其他数据。 我想这样做,因为在微调器中拥有大量数据看起来很糟糕。 我已经看到了对微调器进行编号的方法,以便第一个选择为1,第二个选择为2,所以我相信我应该能够将其用作调用字符串的参考,但是我只在android studio中工作了大约两个个星期,我找不到其他有关此操作的帖子。 有什么建议么?

谢谢你的时间 :)

更新资料

我现在已经找到了一个解决方案,如果我将它包含在活动中,那么现在我可以让TextView引用该字符串,但是我想从strings.xml中提取它,这样可以使事情保持整洁并阻止我重复这么多。 我在下面链接了我的代码。 如果我将“ textnone”替换为“ text1”,它将起作用,因此我认为这是我链接字符串数组的方式中的一个问题。

    <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>

如果我理解正确,则应在布局中的Spinner下面放置其他TextView。 然后使用其他信息(信息数组)创建单独的字符串数组,其大小与现有的字符串数组相同。

然后在微调器的onItemSelected(AdapterView parent,View view,int pos,long id)方法中,获取所选微调器元素(pos)的位置,并使用信息数组(带有索引pos的元素)中的相应元素更新TextView。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM