2013-05-09 70 views
0

如何将按钮添加到android java代码中?它是主要的活动Java代码:将按钮添加到android 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文件?

回答

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)); 

} 
+0

@ romando90:这只是注释分离代码,你还需要添加现有的代码 – 2013-05-09 18:45:20

0

您可以尝试将main_activity.xml设置为内容视图,通过id查找相关布局并使用realtivelayout.addView(新的SingleTouchEventView(this,null))添加。 然后你已经有了你的xml文件和你的singletoucheventview。

在XML文件中找到你的RelativeLayout并添加:

android:id="@+id/relativelayout" 

这只是一个例子。 之后去到你的类和创建对象:

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

之后,你就可以打电话

relativeLayout.addView(View); 
+0

如何使用findViewbyId查找相对布局? – romando90 2013-05-09 18:29:09

+0

刚编辑我以前的帖子。 – 2013-05-09 19:17:44

0

SingleTouchEventView view = new SingleTouchEventView(this,null)

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

view.addView(b); 我无法使用addView方法。未定义.. – romando90 2013-05-09 18:38:04