【问题标题】:Passing a variable between 2 classes在两个类之间传递一个变量
【发布时间】:2014-08-07 07:47:55
【问题描述】:

我有一个以随机颜色绘制线条的程序!所以我做了一个可变的“颜色”和3个按钮。每个按钮都有它的值,这个值设置颜色。但是颜色设置命令在另一个类中,所以我必须将“颜色”值传递给 DrawArea 类。 所以我的问题是,我该怎么做?我用 getter 和 setter 以某种方式尝试过,但失败了......

一个是活动,一个是视图

主要活动

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.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       
public int colour = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawlayout);
    DrawArea da = new DrawArea(this, null);

    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(handler);
    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(handler);
    b3 = (Button) findViewById(R.id.button3);
    b3.setOnClickListener(handler);
}

View.OnClickListener handler = new View.OnClickListener(){

    public void onClick(View v){
        if(v==b1){

        farbe = 1;  
    }
        if(v==b2){

        farbe = 2;  
        }

        if(v==b3){

        farbe = 3;  
        }   
}};   
}

绘图区

package com.example.drawproject;

import android.content.Context;
import android.graphics.*;
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();


int count = 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++;
            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;
}

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

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

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

【问题讨论】:

  • 你要更改 DrawArea 中的哪种颜色??
  • @Rod_Algonquin 当我知道 hoe 获取值时,我将更改paintsetColor(_rdmColor.nextInt());从私人虚空点向下

标签: java android class variables


【解决方案1】:

在您的 MainActivity 中

public class MainActivity extends Activity {

Button b1; //Gelb
Button b2; //Blau
Button b3; //Grün       
public int colour = 0;
@Override

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

    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(handler);
    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(handler);
    b3 = (Button) findViewById(R.id.button3);
    b3.setOnClickListener(handler);

     public static Integer getColorValue() {

      // Assign your color value
      Integer value=Whatever_value _you_want;

      return value
     }
}

在 DrawArea 类中获取该值

public class DrawArea extends View {

       //  get value any where in DrawAreaClass like this
    Integer Value = MainActivity.getColorValue();
}

【讨论】:

  • 嘿,我的朋友,我非常感谢你,你拯救了我的一天!现在可以正常使用了,谢谢!!
  • 代替感谢,您可以投票给答案,这样它也可以帮助其他人
  • 嘿,我又有一个问题。我必须传递一个在每次触摸后不断变化的值,我像这样尝试过,但是值保持不变并且不会改变,我应该改变什么?
【解决方案2】:

我认为您是在询问将值从一个活动传递到另一个活动。那么你需要使用Intent

>在FirstActivity中

Intent intent = new Intent(CurrentActivity.this,ActivityWhereYouWantGo.class);
intent.putExtra("Variable", "value");
startActivity(intent);

当您调用上述行时,您的第二个活动将开始,并且第一个活动的变量值将在第二个活动中可用,您必须这样做

>在第二个活动中

Bundle extras = getIntent().getExtras();
  if (extras != null) {
   String datas= extras.getString("Variable");
   if (datas!= null) {
        // do stuff
   }   

按照您在 cmets 中的要求发送 int 值

在第一次活动中使用

 intent.putExtra("Variable", intvalue);

然后在第二个活动中写

int num=extras.getIntExtra("Variable");

【讨论】:

  • DrawArea 类型的方法 getIntent() 未定义,这就是我得到的错误..
  • 你能解释一下我必须把代码放在哪里吗,因为我这样做了,它不起作用._.
【解决方案3】:

头等舱

intent i = new Intent(firstclass.this, secondclass.class).putextra("Key", "testing");
startActivity(i);

二等舱

String value = getIntent.getExtra("key");

【讨论】:

    【解决方案4】:

    要在两个活动之间传递值,您需要使用这样的意图

    源类:

    Intent intent = new Intent(this, NewActivity.class); 
    myIntent.putExtra("variable ", "value");
    startActivity(intent)
    

    目的地活动:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view);    
    
    Intent intent = getIntent();
    
    String val= intent.getStringExtra("variable");
    
    
    }
    

    在你的情况下,你可以这样做

    在 MaiActivity 中创建一个方法getValue() 像这样

    public int getValue(){
    {
       Int Val;
    
       // assign value here
       // and return it
    
        return Val;
     }
    

    然后从 DrawArea 类中像这样访问它

    Int color_value = MainActivity.getValue();
    

    来自enter link description here的参考

    【讨论】:

    • 其中一个是 View 类没关系?
    • Intent 提供了一种工具,用于在不同应用程序中的代码之间执行后期运行时绑定。它最重要的用途是在活动的启动中,它可以被认为是活动之间的粘合剂。它基本上是一种被动数据结构,包含对要执行的操作的抽象描述
    • 这就像从源活动中抛出值并从目标活动中捕获。
    • 如果你喜欢答案,如果你认为是答案,你可以投票或标记为答案
    • 我在 Intent intent = getIntent(); 行中收到上述答案中的 sam 错误它说方法 getIntent() 对于 DrawArea 类型是未定义的...
    猜你喜欢
    • 2017-10-16
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2017-06-10
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多