0

我需要一些关于如何使用通过我的代码生成的值来更新位于 XML 中的 Textview 的建议。

该程序通过画布绘制一条直线,我希望 textView 反映线的尖端 x 值。

我的代码如下:

package com.example.threadexperiment1;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    lineDrawing InfiniteLine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        InfiniteLine = new lineDrawing (this);
        setContentView(R.layout.activity_main);

        Thread thread = new Thread(InfiniteLine);
        thread.start();

        RelativeLayout RL1 = (RelativeLayout)findViewById(R.id.RelativeLayout1);
        RL1.addView(InfiniteLine);

    }
    public class lineDrawing extends View implements Runnable {

        float x=100, y=100;
        Paint lineColour = new Paint ();
        TextView TV1 = (TextView)findViewById(R.id.textView1);

        //Constructor
        public lineDrawing(Context context) {                               
            super(context);
            lineColour.setColor(Color.BLACK);   
        }

        //ondraw codes
        @Override
        public void onDraw (Canvas canvas){
            canvas.drawLine(0,0,x,y,lineColour);

            if (x==500){
                x=0;
                y=0;
            }

            invalidate();
        }

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {

            try {
            Thread.sleep(250);
                } 

            catch (Exception e) {}
                x+=10;
                y+=10;
            }

        }

    }

}
4

1 回答 1

1

在线程中尝试 textView.setText(string) 。

于 2013-03-08T15:16:13.500 回答