嗨,我正在做一个练习项目,可以用我放置的坐标绘制一条线。屏幕只有两个文本字段和一个按钮。例如,如果我在这两个文本字段中放入“20”和“30”并单击“绘制”按钮,我希望应用程序在另一个视图中从(0,0)到(20,30)绘制一条线。问题是,当我单击按钮时,这两个文本字段中的值将传递到setCoordinates()函数中,但视图不会显示该行。我尝试在canvas.drawLine()函数中更改坐标[0]和坐标[1]到50和50然后该行显示,所以我猜可能是invalidate()函数不起作用?无论如何请帮助找到我的问题所在,谢谢!!!!!这是我的代码:
MazeBuilder.java
package com.example.maze;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.app.Activity;
import android.graphics.Color;
public class MazeBuilder extends Activity {
private DrawMaze drawMaze ;
private EditText editTextX;
private EditText editTextY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawMaze = new DrawMaze(this);
setContentView(R.layout.activity_maze_builder);
editTextX = (EditText) findViewById(R.id.editText1);
editTextY = (EditText) findViewById(R.id.editText2);
}
public void buildMaze(View view){
final int x = getValue(editTextX);
final int y = getValue(editTextY);
drawMaze.setCoordinates(x, y);
}
private static int getValue (EditText text) {
try {
return Integer.parseInt(text.getText().toString());
} catch (NumberFormatException ex) {
return 0;
}
}
}
DrawMaze.java
package com.example.maze;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class DrawMaze extends View{
private Paint paint = new Paint();
private int[] coordinates = new int[2];
//===============================Constructors==============================================
//
//
public DrawMaze(Context context) {
super( context );
}
public DrawMaze(Context context, AttributeSet attrs) {
super( context, attrs );
}
public DrawMaze(Context context, AttributeSet attrs, int defStyle) {
super( context, attrs, defStyle );
}
//===============================Initialize the color of line and background==========================================
//
//
public void init(){
paint.setColor(Color.BLACK);
this.setBackgroundColor(Color.WHITE);
}
public void setCoordinates(int x, int y){
coordinates[0] = x;
coordinates[1] = y;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
System.out.println("=============coordinates[0]:"+coordinates[0]+"================coordinates[1]:"+coordinates[1]+"========");
canvas.drawLine(0, 0, coordinates[0] , coordinates[1] , paint);
}
}
activity_maze_builder.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MazeBuilder" >
<com.example.maze.DrawMaze
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relativeLayout1" />
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="500dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_centerHorizontal="true"
android:text="Columns: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/button1"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:onClick="buildMaze"
android:text="Build" />
<EditText
android:id="@+id/editText1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/textView1"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_toLeftOf="@+id/editText1"
android:text="Rows: "
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
答案 0 :(得分:2)
而不是
drawMaze = new DrawMaze(this);
使用
drawMaze = (DrawMaze)findViewById(R.id.relativeLayout2);
但在
下setContentView()
原因是当您使用布局设置内容视图时,它使用的是布局中的DrawMaze而不是
drawMaze = new DrawMaze(this);
已实例化但从未设置为内容视图