简体   繁体   English

Android Java添加一个textView

[英]Android java Add a textView

I have a java file that its setContentView is to another java file... 我有一个Java文件,其setContentView到另一个Java文件...

Here is the first java file 这是第一个Java文件

    package dalton.metzler.occupied;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;


public class PlayActivity extends Activity{

    Play ourView;

    LinearLayout linear;
    TextView text;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ourView = new Play(this);
        setContentView(ourView);
    }



}

And here is the file thats linking to it as the set contentview there is not speical code you need to look at just showing so you can see what it is. 这是链接到该文件的文件,因为它是set contentview,您不需要查看特定的代码即可显示,这样您就可以看到它是什么。

And see exactly what i mean about the text thing i am trying to do 并确切地了解我对我要执行的文字操作的意思

    package dalton.metzler.occupied;

import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;


    public class Play extends View {

    Bitmap gBall1, gBall2, gBall3, gBall4, gBall5, gBall6, gBall7, gBall8, gBall9, gBall10, gBall11, gBall12, gBall13;

    float changingY, changingY2, changingY3, changingY4, changingY5, changingY6, changingY7, changingY8, changingY9, changingY10, changingY11, changingY12, changingY13;


    public Play(Context context) {
        super(context);


        gBall1 = BitmapFactory.decodeResource(getResources(), R.drawable.mantrans);
        gBall2 = BitmapFactory.decodeResource(getResources(), R.drawable.womentrans);
        gBall3 = BitmapFactory.decodeResource(getResources(), R.drawable.mantrans);
        gBall4 = BitmapFactory.decodeResource(getResources(), R.drawable.mantrans);
        changingY = 0;
        changingY2 = -110;
        changingY3 = -220;
        changingY4 = -330;
        changingY5 = -440;
        changingY6 = -550;
        changingY7 = -660;
        changingY8 = -770;
        changingY9 = -880;
        changingY10 = -990;
        changingY11 = -1100;
        changingY12 = -1210;
        changingY13 = -1320;

    }

    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(gBall1, (canvas.getWidth()/2), changingY, null);
    canvas.drawBitmap(gBall2, (canvas.getWidth()/2), changingY2, null);
    canvas.drawBitmap(gBall3, (canvas.getWidth()/2), changingY3, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY4, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY5, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY6, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY7, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY8, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY9, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY10, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY11, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY12, null);
    canvas.drawBitmap(gBall4, (canvas.getWidth()/2), changingY13, null);
    if (changingY < canvas.getHeight()){
        changingY += 5;

    }else{
        changingY = -600;
    }
    if (changingY2 < canvas.getHeight()){
        changingY2 += 5;

    }else{
        changingY2 = -600;
    }
    if (changingY3 < canvas.getHeight()){
        changingY3 += 5;

    }else{
        changingY3 = -600;
    }
    if (changingY4 < canvas.getHeight()){
        changingY4 += 5;

    }else{
        changingY4 = -600;
    }
    if (changingY5 < canvas.getHeight()){
        changingY5 += 5;

    }else{
        changingY5 = -600;
    }
    if (changingY6 < canvas.getHeight()){
        changingY6 += 5;

    }else{
        changingY6 = -600;
    }
    if (changingY7 < canvas.getHeight()){
        changingY7 += 5;

    }else{
        changingY7 = -600;
    }
    if (changingY8 < canvas.getHeight()){
        changingY8 += 5;

    }else{
        changingY8 = -600;
    }
    if (changingY9 < canvas.getHeight()){
        changingY9 += 5;

    }else{
        changingY9 = -600;
    }
    if (changingY10 < canvas.getHeight()){
        changingY10 += 5;

    }else{
        changingY10 = -600;
    }
    if (changingY11 < canvas.getHeight()){
        changingY11 += 5;

    }else{
        changingY11 = -600;
    }
    if (changingY12 < canvas.getHeight()){
        changingY12 += 5;

    }else{
        changingY12 = -600;
    }
    if (changingY13 < canvas.getHeight()){
        changingY13 += 5;

    }else{
        changingY13 = -600;
    }
    invalidate();   
    }       
}

How can i add text to the app? 如何向应用程序添加文本? Like a textview but not in the xml because i can't do this (i think) I want to add like the text: Score: 0 to the screen 像textview一样,但不在xml中,因为我做不到(我认为),我想像文本一样添加:得分:0到屏幕

Right now you cannot added another view to your custom View as it extends the View class. 现在,您不能将另一个视图添加到自定义视图中,因为它扩展了View类。 You will need to extend a viewgroup class to add other views. 您将需要扩展一个viewgroup类以添加其他视图。

Create a linear layout and add the play view and the textview to that and set the linear layout in the setContentView function. 创建一个线性布局,并向其中添加播放视图和textview,然后在setContentView函数中设置线性布局。

If you just need to draw text on playview, there is the canvas.drawText function. 如果只需要在播放视图上绘制文本,则可以使用canvas.drawText函数。

I think your drawText method is wrong. 我认为您的drawText方法是错误的。 Try this. 尝试这个。

    Paint orangePaint = new Paint();
    orangePaint.setColor(Color.BLACK);

    canvas.drawText("test", 50, 50, orangePaint);

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM