如何在另一个活动类中打开View类?

时间:2014-08-04 16:27:35

标签: android android-activity view

我有一个名为Feldlayout.class的活动类,当这个类被调用时,它会打开布局:Feldlayout.xml,我有一个ViewFeld.class视图应该在Feldlayout.class中打开。 我怎样才能做到这一点?代码,提示&请求的想法!

Feldlayout.class

package com.example.volleyballapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class feldlayout extends Activity{
Button b7;   

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feldlayout);
        b7 = (Button) findViewById(R.id.button7);
        b7.setOnClickListener(handler);

   }

  View.OnClickListener handler = new View.OnClickListener(){
    public void onClick(View v) {

        if(v==b7){
        Intent i = new Intent(feldlayout.this, Main_Layout.class);
        startActivity(i);
        }


    }};}

Feldlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/farbe"
android:orientation="vertical" >

<ImageView
  android:id="@+id/view2"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginBottom="70dp"
  android:layout_marginLeft="70dp"
  android:layout_marginRight="70dp"
  android:layout_marginTop="160dp"
  android:background="@drawable/whiterectangle" />

 <ImageView
     android:id="@+id/view1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_marginBottom="74dp"
     android:layout_marginLeft="74dp"
     android:layout_marginRight="74dp"
     android:layout_marginTop="164dp"
     android:background="@drawable/volleyballfeld" />

 <ImageView
     android:id="@+id/view3meterlinie"
     android:layout_width="match_parent"
     android:layout_height="4dp"
     android:layout_alignTop="@+id/view1"
     android:layout_marginLeft="70dp"
     android:layout_marginRight="70dp"
     android:layout_marginTop="100dp"
     android:background="@drawable/dreimeterlinie" />

 <ImageView
     android:id="@+id/netz"
     android:layout_width="match_parent"
     android:layout_height="4dp"
     android:layout_alignTop="@+id/view1"
     android:layout_marginLeft="20dp"
     android:layout_marginRight="20dp"
     android:layout_marginTop="310dp"
     android:background="@drawable/netz" />

 <ImageView
     android:id="@+id/view3meterliniedef"
     android:layout_width="match_parent"
     android:layout_height="4dp"
     android:layout_alignTop="@+id/view1"
     android:layout_marginLeft="70dp"
     android:layout_marginRight="70dp"
     android:layout_marginTop="520dp"
     android:background="@drawable/dreimeterlinie" />

 <Button
     android:id="@+id/button8"
     style="@style/HomeText"
     android:layout_width="140dp"
     android:layout_height="85dp"
     android:layout_alignParentTop="true"
     android:layout_alignRight="@+id/view1"
     android:layout_marginTop="1dp"
     android:background="@drawable/bnt_black"
     android:text="Speichern" />

 <Button
     android:id="@+id/button7"
     style="@style/HomeText"
     android:layout_width="140dp"
     android:layout_height="85dp"
     android:layout_alignLeft="@+id/view2"
     android:layout_alignParentTop="true"
     android:layout_marginTop="1dp"
     android:background="@drawable/bnt_black"
     android:onClick="home"
     android:text="Zurück" />

 <Button
     android:id="@+id/button9"
     style="@style/HomeText"
     android:layout_width="140dp"
     android:layout_height="85dp"
     android:layout_alignTop="@+id/button7"
     android:layout_marginLeft="35dp"
     android:layout_toRightOf="@+id/button7"
     android:background="@drawable/bnt_black"
     android:text="Linien Anzeigen" />

 <Button
     android:id="@+id/Statistik"
     style="@style/HomeText"
     android:layout_width="140dp"
     android:layout_height="85dp"
     android:layout_alignBaseline="@+id/button8"
     android:layout_alignBottom="@+id/button8"
     android:layout_marginRight="31dp"
     android:layout_toLeftOf="@+id/button8"
     android:background="@drawable/bnt_black"
     android:text="Statistik" />

    </RelativeLayout>

ViewFeld.class

package com.example.volleyballapp;

 import android.content.Context;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
 import android.graphics.Path;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.View;

 public class ViewFeld extends View{

 private Paint paint = new Paint();
  private Path path = new Path();
  int count = 1;

public ViewFeld(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);           //Breite des Strichs
    paint.setColor(Color.BLACK);        //Farbe des Strichs 
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);

  }



protected void onDraw(Canvas canvas) {

    if ((count%2 != 0)){
    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:

        count++;

        if(count%2 != 1){
        path.moveTo(eventX, eventY);
      return true;
        }
        if(count%2 != 0){
        path.lineTo(eventX, eventY);
        break;
        }
    case MotionEvent.ACTION_MOVE:

      break;
    case MotionEvent.ACTION_UP:
      // nothing to do
      break;
    default:
      return false;
    }

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




}

0 个答案:

没有答案