将正在变化的值从一个类传递到另一个类

时间:2014-08-08 13:09:51

标签: android android-intent android-activity view

我有价值"数"在我的View类中,我有我的MainActivity,它正在请求"计数"价值,这是正常的。但价值"计数"每当触摸屏幕时,这个值都在增加,所以这个值会改变,但是在MainActivity中,我总是得到" count"来自第一个请求的值,而不是不同的增加" count"。我想知道,我怎么能管理这个请求总是得到新的"计数"值..

MainActivity

package com.example.drawproject;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends Activity {

Button b1; //Gelb
Button b2; //Blau
Button b3; //Grün       


Integer Pcount2 = DrawArea.getCountValue(); //get the count value

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawlayout);
DrawArea da = new DrawArea(this, null);

if(Pcount2 % 2 == 1){ //use it -> it's always the same number, instead of the increased
    Intent nextScreen = new Intent(this, ChangeArea.class);
    startActivity(nextScreen);
}
else{
    Pcount2++;

}

}
}

ViewClass

package com.example.drawproject;

import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DrawArea extends View {

private List<Stroke> _allStrokes; //all strokes that need to be drawn
private SparseArray<Stroke> _activeStrokes; //use to retrieve the currently drawn strokes
private Random _rdmColor = new Random();


static int count = 1; // count is 1

public DrawArea(Context context, AttributeSet attrs) {
super(context, attrs);

_allStrokes = new ArrayList<Stroke>();
_activeStrokes = new SparseArray<Stroke>();
setFocusable(true);
setFocusableInTouchMode(true);

}


public void onDraw(Canvas canvas) {
if (_allStrokes != null) {
    for (Stroke stroke: _allStrokes) {
        if (stroke != null) {
            Path path = stroke.getPath();
            Paint painter = stroke.getPaint();
            if ((path != null) && (painter != null)) {
                if(count%2 != 0){
                canvas.drawPath(path, painter);
                }
            }
        }
    }
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getActionMasked();
final int pointerCount = event.getPointerCount();

switch (action) {
    case MotionEvent.ACTION_DOWN: {
        count++; // count increases +1
        if(count%2 != 1)
        {pointDown((int)event.getX(), (int)event.getY(), event.getPointerId(0));
        break;
        }
        if (count%2 != 0){
            for (int pc = 0; pc < pointerCount; pc++) {
                pointDown((int)event.getX(pc), (int)event.getY(pc), event.getPointerId(pc));
    }
        }
    }
    case MotionEvent.ACTION_MOVE: {

        break;
    }

    case MotionEvent.ACTION_UP: {
        break;
    }

}
invalidate();
return true;
}

public static Integer getCountValue(){

Integer count2 = count; //get the count value to pass it to the other class
return count2;


}

private void pointDown(int x, int y, int id) {

Integer value = ChangeArea.getColorValue();

if(count%2 !=1){
//create a paint with random color
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);

if (value == 1){

paint.setStrokeWidth(15);
    }else
    if (value == 2){

        paint.setStrokeWidth(20);
        }else
    if (value == 3){

        paint.setStrokeWidth(5);
        }
    else{

    paint.setStrokeWidth(10);
    }

    if (value == 1){

    paint.setColor(Color.YELLOW);
    }else
    if (value == 2){

        paint.setColor(Color.BLUE);
        }else
    if (value == 3){

        paint.setColor(Color.GREEN);
        }
    else{
        paint.setColor(_rdmColor.nextInt()); //Here should the values be added!
    }
//create the Stroke
Point pt = new Point(x, y);
Stroke stroke = new Stroke(paint);
stroke.addPoint(pt);
_activeStrokes.put(id, stroke);
_allStrokes.add(stroke);
}

if (count%2 != 0){
//retrieve the stroke and add new point to its path
Stroke stroke = _activeStrokes.get(id);
if (stroke != null) {
    Point pt = new Point(x, y);
    stroke.addPoint(pt);
}
}
}
}

1 个答案:

答案 0 :(得分:0)

您可以实现侦听器接口。 在视图中定义界面:

```public interface ViewCountListener {
     void onViewCountChanged(int viewCount);
}```

在您的视图中添加变量ViewCountListener listener;和方法void addListener(ViewCountListener listener),您可以在其中设置监听器。

并在onTouch中添加:

@Override
public boolean onTouchEvent(MotionEvent event) {
    //your code

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            count++; // count increases +1
            if (listener != null) listener.onViewCountChange(count);

之后添加到您的活动implements ViewCountListener,实施onViewCountChange(int)方法并添加一个包含该计数器的字段成员。

优点:

  • 任何类(不仅是活动)都可以成为监听者
  • 您的活动可以成为您创建的所有观看次数的聆听者

缺点:

  • 仅当您的观点属于您的活动环境时,它才会起作用。但是,您可以在activity static中创建make holding变量,并将其值传递给其他活动。

希望有所帮助)