2

如果我想在这个类中添加一个按钮以便我可以调用onclicklistener,我应该怎么做?

public class GameView extends View {
    Path circle;
    Paint cPaint;
    Paint tPaint;
    String z;
    int i = 65, strt, arc, leftx, topy, rightx, bottomy, maxx, maxy;
    boolean flag1, flag2, flag3;
    double n1, n2;
    int n, n3 = 180,n4,n5 = 90;
    float f1 = 180, f2 = 90;
    Button b1;
    Random r = new Random();
    RectF  oval;

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

        leftx = 0;
        topy = 60;
        rightx = 150;
        bottomy = 120;

        z = String.valueOf(Character.toChars(i));

        cPaint = new Paint();
        cPaint.setColor(Color.RED);

        strt = 45;
        arc = 315;

        n1 = Math.random() * 600;
        Log.d("random", z);

        if (flag2 == false)
            new DrawThread(this);

        // cPaint.setStrokeWidth(2);
        tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        tPaint.setColor(Color.BLACK);
        float scale = getResources().getDisplayMetrics().scaledDensity;
        tPaint.setTextSize(20 * scale);
    }

    public void onSizeChanged(int w,int h,int oldh,int oldw) {
        maxx = oldw;
        maxy = oldh;
    }

    //@Override
    protected void onDraw(Canvas canvas) {
        // Drawing commands go here
        oval = new RectF(leftx,topy,rightx,bottomy);
        canvas.drawArc(oval, strt, arc, true, cPaint);
        while (i < 90)  {
            canvas.drawText(String.valueOf(Character.toChars(i)),f1,f2, tPaint);
            break;
        }
    }
}
4

4 回答 4

2
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

在下面添加上面setContentView

setContentView(R.layout.activity_new_game);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

您需要使用设置布局setContentView,然后初始化视图。

请注意,您可以findViewById将当前视图层次结构设置为活动

编辑:

示例:这是一个示例。如果您执行以下类似操作,我不明白为什么它不起作用。

<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=".NewGame" 
android:id="@+id/relative_layout"
>

<Button
    android:id="@+id/prev_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/next_button"
    android:layout_alignBottom="@+id/next_button"
    android:layout_centerHorizontal="true"
    android:text="stop_label" />

  <Button
    android:id="@+id/next_button"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginRight="20dp"
    android:layout_toLeftOf="@+id/prev_button"
    android:text="start_label" />

  <RelativeLayout
      android:layout_width="wrap_content"
      android:id="@+id/rl"
      android:layout_below="@+id/prev_button"
      android:layout_height="wrap_content"

       >
  </RelativeLayout>

 </RelativeLayout>

活动

public class MainActivity extends Activity {
    private Button btn1;
    private EditText edt1, edt2, edt3, edt4 ,edt5, edt6;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyView mv = new MyView(this);
        setContentView(R.layout.activity_main);
        RelativeLayout layout= (RelativeLayout) findViewById(R.id.rl);
        layout.addView(mv);
    }
    class MyView extends View
    {

        public MyView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            canvas.drawColor(Color.RED);
        }

    }
}

快照

在此处输入图像描述

于 2013-07-18T08:05:43.097 回答
1

在尝试找到您的相对布局之前,您需要放大您的视图。尝试 :

RelativeLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    gameview=new GameView(this);
    setContentView(R.layout.activity_new_game);
    layout = (RelativeLayout) findViewById(R.id.relative_layout);

    //LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //view = mInflater.inflate(R.layout.activity_new_game, gameview, true);

    layout.addView(gameview);

}
于 2013-07-18T08:05:38.833 回答
0

你没有layout在你的代码中初始化,所以首先初始化layoutsetContentView然后将视图添加到你的layout

RelativeLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_game);
    //Initialize game view
    GameView  gameview=new GameView(this);
    //initialize layout
    layout = (RelativeLayout) findViewById(R.id.relative_layout);
    //adding game view to layout
    layout.addView(gameview);
}

编辑:

之前像这样初始化setContentView

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

是错误的方法你必须setContentView像上面的代码一样初始化视图

于 2013-07-18T08:25:37.260 回答
0
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

findViewById你之前不能打电话setContentView。移动

RelativeLayout layout = (RelativeLayout) findViewById(R.id.relative_layout);

setContentView(R.layout.activity_new_game);

于 2013-07-18T08:05:22.730 回答