0

を呼び出すことができるように、このクラスにボタンを追加したい場合onclicklistener、どのようにすればよいですか?また、このビューを追加するアクティビティ クラスも用意しました。

アクティビティ:

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;

import android.content.Context;



public class NewGame extends Activity {

View view;
Context context;
RelativeLayout layout;
GameView gameview;

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



    //layout.addView(gameview);

}


}

view: public class GameView extends View { Path circle; ペイント cPaint; ペイント tPaint; 文字列 z; int i = 65、strt、arc、leftx、topy、rightx、bottomy、maxx、maxy; ブール flag1、flag2、flag3; ダブル n1、n2; int n、n3 = 180、n4、n5 = 90; float f1 = 180、f2 = 90; ボタン b1; ランダム r = 新しいランダム (); RectF 楕円;

    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

2 に答える 2

1

次のように実行できます。

Button bt = new Button(this);
bt.setText("A Button");
bt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
linerLayout.addView(bt);

そして、あなたはこれを行うことができます

bt.setOnClickListener(new View.OnClickListener() {

//TO DO
}

これがあなたのお役に立てば幸いです。

于 2013-07-18T12:01:29.710 に答える
0

まず、カスタム ビューで addView(View v) メソッドを使用できるようにするには、View ではなく ViewGroup を拡張する必要があります。その後、このコードを使用できます

b1=new Button(context);
b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT ));
b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        });
        this.addView(b1);
于 2013-07-18T12:05:38.740 に答える