在屏幕上延迟显示文本(Android Studio)

m3eecexj  于 2023-09-29  发布在  Android
关注(0)|答案(1)|浏览(125)

大家好,我最近开始在Android Studio中进行开发,遇到了一个问题。我正在开发一个文本小说类比与生命线。底线是故事的文本显示在屏幕上有轻微的延迟。我希望游戏的玩法就像在信使聊天。当阵列中的文本完全显示在屏幕上时,应显示2到5个按钮,可选择操作。
现在我将描述这个问题。为了显示文本,我使用了ListView组件。如果我在循环中添加1秒的延迟,那么文本将不会按顺序显示,而是完全显示。我希望文本以1秒的延迟顺序显示。我试着用一个处理器,它帮助。但是,当我想在循环结束后显示按钮时,它不起作用,按钮会立即显示。我明白为什么会这样。
但我有一个问题--有可能放弃使用多线程吗?实现我的想法的最好方法是什么?
主要类别:

public class GameMain extends AppCompatActivity {
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_main);

        ListView mainList = findViewById(R.id.list_of_text);
        //NewGame newGame = new NewGame();
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);

        String[] listText = {
                "Text1",
                "Text2",
                "Text3",
        };

        mainList.setAdapter(adapter);
        SetTextOnDisplay textOnDisplay = new SetTextOnDisplay();
        textOnDisplay.displayTextSequentially(listText, adapter);

        Button button1 = findViewById(R.id.button_main_1);
        button1.setVisibility(View.VISIBLE);
    }
}

显示文本的类:

public class SetTextOnDisplay extends Thread {
    public void displayTextSequentially(final String[] listText, final ArrayAdapter<String> adapter) {
        final Handler handler = new Handler();
        int delay = 500;

        for (int i = 0; i < listText.length; i++) {
            final int index = i;
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    adapter.add(listText[index]);
                    if (index == listText.length) {
                        //Button button1 = findViewById(R.id.button_main_1);
                        //button1.setVisibility(View.VISIBLE);
                    }
                }
            }, delay * i);
        }
    }
}
iih3973s

iih3973s1#

尝试像这样使用处理程序:

public class GameMain extends AppCompatActivity {
    private ListView mainList;
    private ArrayAdapter<String> adapter;
    private String[] listText;
    private int currentIndex = 0;
    private Button button1;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_main);

        mainList = findViewById(R.id.list_of_text);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
        listText = new String[] {
            "Text1",
            "Text2",
            "Text3",
        };

        mainList.setAdapter(adapter);
        button1 = findViewById(R.id.button_main_1);
        button1.setVisibility(View.INVISIBLE);

        displayTextWithDelay();
    }

    private void displayTextWithDelay() {
        if (currentIndex < listText.length) {
            adapter.add(listText[currentIndex]);
            currentIndex++;

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    displayTextWithDelay();
                }
            }, 1000);
        } else {
            button1.setVisibility(View.VISIBLE);
        }
    }
}

相关问题