Android:使用新值不断更新TextView

时间:2013-02-15 06:15:33

标签: textview

我是Java和android的新手,我需要一些建议来了解如何使用我的代码生成的值更新位于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;
            }

        }

    }

}

1 个答案:

答案 0 :(得分:1)

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