You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android问卷界面如何实现仅显示当前与上一个问题?

Hey there! Let's figure out how to make your Android quiz app only show the current question plus the previous one—right now your layout dumps all questions at once, so we need to adjust that. Below are two practical solutions tailored to your needs:

解决方案思路

The core idea is to dynamically control which question views are visible—either by toggling visibility of pre-defined views (good for small question sets) or creating views on the fly (better for large/remote question lists).

方案1:Quick Fix for Existing XML (Small Question Sets)

If you only have a handful of questions, you can tweak your current layout to hide/show question groups as needed:

Step 1: Wrap Each Question in a Container

First, enclose every question (TextView + button row) in its own LinearLayout so we can target each group individually:

<!-- Question 1 Group -->
<LinearLayout
    android:id="@+id/question_group_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Caterpillars turn into butterflies." />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button android:id="@+id/button5" ... android:text="true" />
        <Button android:id="@+id/button6" ... android:text="false" />
    </LinearLayout>
</LinearLayout>

<!-- Repeat for Question 2, 3, etc. -->
<LinearLayout
    android:id="@+id/question_group_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView android:id="@+id/textView9"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bubble gum contains rubber." />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button android:id="@+id/button7" ... android:text="true" />
        <Button android:id="@+id/button8" ... android:text="false" />
    </LinearLayout>
</LinearLayout>

Step 2: Control Visibility in Code

In your Activity/Fragment, track the current question index and toggle visibility when the user answers:

private val questionGroups = mutableListOf<LinearLayout>()
private var currentQuestionIndex = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.your_layout)

    // Add all question groups to our list
    questionGroups.add(findViewById(R.id.question_group_1))
    questionGroups.add(findViewById(R.id.question_group_2))
    questionGroups.add(findViewById(R.id.question_group_3))
    // Add remaining groups here

    // Start by showing only the first question
    questionGroups.forEach { it.visibility = View.GONE }
    questionGroups[currentQuestionIndex].visibility = View.VISIBLE
}

// Call this when the user taps a true/false button
private fun onAnswerSubmitted() {
    // Handle answer logic (scoring, etc.) here

    val previousIndex = currentQuestionIndex
    currentQuestionIndex = min(currentQuestionIndex + 1, questionGroups.size - 1)

    // Hide all groups, then show previous + current
    questionGroups.forEach { it.visibility = View.GONE }
    questionGroups[previousIndex].visibility = View.VISIBLE
    questionGroups[currentQuestionIndex].visibility = View.VISIBLE
}

方案2:Dynamic View Creation (Large/Remote Question Lists)

If you have lots of questions or load them from a server, this approach is more memory-efficient—we only keep the current and previous question views in the layout:

Step 1: Simplify Your Base Layout

Strip down your XML to just a title and a container for dynamic questions:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            
            <!-- Page Title -->
            <TextView android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#3f51b5"
                android:text="MY PAGE TITLE"
                android:textColor="#fff"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            
            <!-- Dynamic questions will be added here -->
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Step 2: Create a Question Item Template

Make a reusable question_item.xml for each question:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">
    
    <TextView
        android:id="@+id/question_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"/>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="8dp">
        
        <Button
            android:id="@+id/true_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="true"/>
        
        <Button
            android:id="@+id/false_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="false"/>
    </LinearLayout>
</LinearLayout>

Step 3: Load Questions Dynamically in Code

// Sample question data (replace with your actual data)
private val questions = listOf(
    "Caterpillars turn into butterflies.",
    "Bubble gum contains rubber.",
    "The deadliest earthquake of 2017 took place in Iran and Iraq.",
    "Electrons are larger than molecules.",
    "Thunderstorms have particular sound frequencies that can hurt a dog’s ears."
)
private var currentQuestionIndex = 0
private val mainContainer: LinearLayout by lazy { findViewById(R.id.main_container) }
private var previousQuestionView: View? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.your_layout)
    loadCurrentQuestion()
}

private fun loadCurrentQuestion() {
    // Inflate the question template
    val questionView = LayoutInflater.from(this).inflate(R.layout.question_item, mainContainer, false)
    val questionText = questionView.findViewById<TextView>(R.id.question_text)
    val trueBtn = questionView.findViewById<Button>(R.id.true_btn)
    val falseBtn = questionView.findViewById<Button>(R.id.false_btn)

    // Set question text
    questionText.text = questions[currentQuestionIndex]

    // Set up button clicks
    trueBtn.setOnClickListener { handleAnswer(true) }
    falseBtn.setOnClickListener { handleAnswer(false) }

    // Add to container
    mainContainer.addView(questionView)

    // Dim the previous question to distinguish it
    previousQuestionView?.alpha = 0.6f
    // Update reference to current view (will be "previous" next time)
    previousQuestionView = questionView
}

private fun handleAnswer(isCorrect: Boolean) {
    // Handle scoring/feedback here

    currentQuestionIndex++
    if (currentQuestionIndex < questions.size) {
        // Remove the oldest question if we have more than 2 visible
        if (mainContainer.childCount > 2) {
            mainContainer.removeViewAt(1) // Index 0 is the title
        }
        loadCurrentQuestion()
    } else {
        // Quiz finished—show results
        showQuizResult()
    }
}

private fun showQuizResult() {
    mainContainer.removeAllViews()
    val resultText = TextView(this).apply {
        text = "Quiz Complete! Your score: [insert score here]"
        textSize = 20sp
        gravity = Gravity.CENTER
        setPadding(16, 64, 16, 64)
    }
    mainContainer.addView(resultText)
}

Bonus Tips

  • Add a subtle fade or slide animation when switching questions to make the transition smoother
  • For the dynamic approach, wrap your question data in a data class (e.g., data class Question(val text: String, val correctAnswer: Boolean)) to manage answers easier
  • If you want to let users scroll back to the previous question, add a "Back" button that adjusts the index and updates visibility accordingly

内容的提问来源于stack exchange,提问作者shreya mehra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:28:36