【问题标题】:Populate TextView with different strings for different levels使用不同级别的不同字符串填充 TextView
【发布时间】:2014-01-03 23:29:13
【问题描述】:

我正在制作一个问答游戏,每个级别都有一个问题的文本视图和一个文本字段供您输入答案。会有很多关卡,所以与其拥有大量的 Java 类和 XML 文件,我想知道是否有更快的解决方案?

我正在考虑使用一个 Java 类来填充相同的 TextView,并为不同的级别使用不同的字符串。这可能吗?我想我可能必须使用数据库?

我现在的代码是这样的(XML):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="fill_horizontal"
android:layout_margin="25dp"
android:orientation="vertical" >

<TextView
    android:id="@+id/tutorialWelcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/tutorial_step_four_a"
    android:textSize="@dimen/text_size"
    android:textStyle="bold" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/enterButton"
    android:hint="@string/answerhere"
    android:inputType="text"
    android:textSize="@dimen/text_size" >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tutorialWelcome"
    android:layout_centerHorizontal="true"
    android:text="@string/tutorial_step_four_b"
    android:textSize="@dimen/text_size" />

<ImageButton
    android:id="@+id/enterButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:contentDescription="@string/nextbutton"
    android:src="@drawable/ic_action_send_now" 
    style="?android:attr/borderlessButtonStyle"/>

任何帮助将不胜感激!

【问题讨论】:

  • 似乎您需要创建自定义视图。和公共函数/构造函数来设置文本视图的文本
  • 你是如何在没有使用数据库的情况下编写一个问答游戏的?! O_o - 是的,DB 起来。有一些代表数据库结构的自定义类,只需调用: textView.setText(myObj.getQuestionText(iLevelNum); 或类似的东西。

标签: java android xml textview populate


【解决方案1】:

有几个选项:

  • 将您的问题存储在 strings.xml 中,然后使用 getString(R.string.id_of_question_you_want) 获取问题并稍后显示它

  • 将问题存储在 SQLite 数据库中

  • 将问题存储在文件中,您需要编写函数来读取文件

编辑:

你也可以在xml中使用字符串数组,然后根据当前显示的是哪个questin,从数组中填充,例如:

  • 在 questions.xml 中定义问题数组:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="questions_array">
            <item>Question 1 content</item>
            <item>Question 2 content</item>
            <item>Question 3 content</item>
            <item>Question 4 content</item>
        </string-array>
    </resources>
    
  • 在您的活动中,您可以将这些问题放在一个数组中:

    Resources res = getResources();
    String[] questions = res.getStringArray(R.array.questions_array);
    
  • 然后,跟踪现在应该显示哪个问题,并从填充布局的活动中的数组中获取字符串:

    int questionNumber = 5;
    
    textViewQuestion.setText(questions[questionNumber]);
    

请记住检查边界,否则如果您的 questionNumber 大于数组中的问题数,您将收到异常。请记住,Java 中的数组从索引 0 开始。

可能还有其他...

查看the docs 了解有关存储数据的信息。

【讨论】:

  • 这是有道理的。虽然我有点困惑。你认为我需要一堆“if”语句来设置条件吗?例如,如果这是第一级,使用第一级问题?如果是这样,您认为我可以如何实现。
  • 另一个 - 可能更有用的 - 选项是将问题存储在 xml 文件中的数组中。请参阅我的更新答案。然后在您的活动中,您可以使用索引从数组中获取正确的问题。
猜你喜欢
  • 2018-05-27
  • 2016-10-31
  • 1970-01-01
  • 2013-04-26
  • 2018-08-14
  • 2017-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多