添加按钮到android java代码

时间:2013-05-09 18:11:47

标签: java android xml button

如何在android java代码中添加按钮?它是主要活动java代码:

    package com.example.pafima_trial;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;

    public class MainActivity extends Activity {

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

    setContentView(new SingleTouchEventView(this, null));
//  setContentView(R.layout.activity_main);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
        }

    }

是SingleTouchEventView.java

    public class SingleTouchEventView extends View {
      private Paint paint = new Paint();
      private Path path = new Path();
      boolean touched = false;
      float x =0;
      float y =0;
      float [] inputx = new float[200];
      float [] inputy = new float[200];
      String [] direction = new String [200];
      int count =0;
      int dcount =0;

      public SingleTouchEventView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
      }

      @Override
      protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
      }

      @Override
      public boolean onTouchEvent(MotionEvent event) {

        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
          path.moveTo(eventX, eventY);
          return true;
        case MotionEvent.ACTION_MOVE:
          path.lineTo(eventX, eventY);

    touched=true;
          inputx[count]=eventX;
          inputy[count]=eventY;
          System.out.println("x is: "+ inputx[count]);
          System.out.println("y is: "+inputy[count]);
          if(count>=2 && count%2 != 1){

              if(inputx[count-2]-inputx[count]<=-15){
          if(inputy[count-2]-inputy[count]<=-15){
              direction[dcount]="right down";
              dcount++;
          }
          if(inputy[count-2]-inputy[count]>15){
              direction[dcount]="right up";
              dcount++;
          }
          if(-14<=inputy[count-2]-inputy[count] && inputy[count-2]-inputy[count]<=15 ){
              direction[dcount]="right";
              dcount++;
          }
      }

      if(inputx[count-2]-inputx[count]>15){
          if(inputy[count-2]-inputy[count]>=15){
              direction[dcount]="left up";
              dcount++;
          }
          if(inputy[count-2]-inputy[count]<-15){
              direction[dcount]="left down";
              dcount++;
          }
          if(15>inputy[count-2]-inputy[count] && inputy[count-2]-inputy[count]>=-15 ){
              direction[dcount]="left";
              dcount++;
          }
      }

      if (inputx[count-2]-inputx[count]<=15 && inputx[count-2]-inputx[count]>-15){
          if(inputy[count-2]-inputy[count]<-15){
              direction[dcount]="down";
              dcount++;
          }
          if(inputy[count-2]-inputy[count]>=15){
              direction[dcount]="up";
              dcount++;
          }

      }
  }
  count++;
  break;
case MotionEvent.ACTION_UP:
    System.out.println("count is "+count);
  break;
default:
  return false;
}
    int a =0;
    while(a<dcount){
        System.out.println("direction["+a+"] is: "+ direction[a]);

        a++;
    }
// Schedules a repaint.
invalidate();
return true;
      }


    } 

我无法在主要活动java代码中添加boutton,我无法将活动主xml文件设置为内​​容视图。 我也可以在java代码中给出xml文件的链接吗?

3 个答案:

答案 0 :(得分:0)

您可以尝试将main_activity.xml设置为内容视图,通过id查找relativelayout并使用realtivelayout.addView(new SingleTouchEventView(this,null))添加。 然后你就得到了你的xml文件和你的单一视图。

在xml文件中找到YourLiveLayout并添加:

android:id="@+id/relativelayout"

这只是一个例子。 之后转到Your class并创建Object:

RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relativelayout);

之后你就可以打电话了

relativeLayout.addView(View);

答案 1 :(得分:0)

您可以使用getWindow().addContentView将主要活动布局中的另一个视图添加为:

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

    setContentView(new SingleTouchEventView(this, null));
    //add xml layout to Activity Window
    LayoutInflater inflater = getLayoutInflater();
    getWindow().addContentView(inflater.inflate(R.layout.activity_main, null),
                              new ViewGroup.LayoutParams(
                               ViewGroup.LayoutParams.MATCH_PARENT,
                               ViewGroup.LayoutParams.MATCH_PARENT));

}

答案 2 :(得分:0)

SingleTouchEventView view = new SingleTouchEventView(this,null)

 setContentView(view);
 Button b = new Button(MainActivity.this);
 view.addView(b);