安卓点击屏幕读取下一行文本

最近在做个人小游戏,希望实现:

1 修改文本时直接修改raw中的txt文件,无需调整代码

2 每次点击屏幕直接修改textview中的显示

3 读文件在子线程中完成,每次循环后挂起等待,点击事件后继续运行

4 尽可能不使用过时的功能

查了很多资料之后东拼西凑地完成了,代码如下:

这是xml,没什么好说的,唯一坑是不能用margin应该用padding,不信可以改了看

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/startlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    android:onClick="screenClick"
    tools:context=".GameStartActivity">

    <TextView
        android:id="@+id/startView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="40dp"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

下面是Java,本人新手,实现的可能比较麻烦,但是确实能跑而且不卡

txt文件在raw里面,就那个R.raw.gamestart,要改成自己的文件名

注:加载出来如果是乱码说明txt不是utf8编码,换成gbk试试,或者检查一下自己的txt编码方式

package com.example.towerapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
//这堆import会自动添加的不用管,使用的话从下面开始复制

public class GameStartActivity extends AppCompatActivity {
    public TextView tv1;
    public View startlayout;
    private Handler mHandler;
    private String textLine = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_start);
        tv1 = findViewById(R.id.startView);
        startlayout = findViewById(R.id.startlayout);

        myThread mythread = new myThread();
        mythread.start();

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                //super.handleMessage(msg);
                if (message.what == 0x123) {
                    synchronized (mythread) {
                        mythread.notify();//唤醒
                    }
                } else if (message.what == 0x321) {//点击屏幕读取文本
                    tv1.setText(textLine);
                }
                //else if (message.what == xxx) {
                    //文本结束后需要进行的下一步行动,例如跳转到另一个activity
                //}
                return false;
            }
        });
    }

    public void screenClick(View view) {
        mHandler.sendEmptyMessage(0x123);
    }

    public class myThread extends Thread {
        @Override
        public void run() {

            Resources res = getResources();
            //找到raw里的文件
            InputStream file = getResources().openRawResource(R.raw.gamestart);
            InputStreamReader inputStreamReader = null;
            try {
                inputStreamReader = new InputStreamReader(file, "utf-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }

            BufferedReader br = new BufferedReader(inputStreamReader);
            StringBuilder stringBuilder = new StringBuilder("");
            try {
                textLine = br.readLine();
                //逐行读取,读到文本结尾结束循环
                while (textLine != null) {
                    stringBuilder.append(textLine);
                    synchronized (this) {
                        try {
                            wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    textLine = br.readLine();
                    //Log.e("TAG", "run: " );
                    mHandler.sendEmptyMessage(0x321);
                    //子线程不能直接修改ui,给主线程发消息
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                br.close();//手动关闭br
                //Log.e("TAG", "run: br is closed");
                //mHandler.sendEmptyMessage(xxx);//用于表明文本结束,可进行下一步
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            super.run();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);//释放handler
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值