我有一个以随机颜色绘制线条的程序!所以我做了一个变量“颜色”和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;
}
}};
}
DrawArea
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);
}
}
}
}
4 个答案:
答案 0 :(得分:1)
我认为您要求将值从一个活动传递到另一个活动。然后你需要使用Intent
<强>&GT;在FirstActivity中
Intent intent = new Intent(CurrentActivity.this,ActivityWhereYouWantGo.class);
intent.putExtra("Variable", "value");
startActivity(intent);
当您调用上述行时,您的第二个活动将开始,并且第一个活动的变量值将在第二个活动中可用,以获得您必须执行的操作
&gt;在第二项活动中
Bundle extras = getIntent().getExtras();
if (extras != null) {
String datas= extras.getString("Variable");
if (datas!= null) {
// do stuff
}
用于发送评论中提到的int值
在第一次活动中使用
intent.putExtra("Variable", intvalue);
,在第二项活动中写
int num=extras.getIntExtra("Variable");
答案 1 :(得分:1)
中的
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 Class中获取此值
public class DrawArea extends View {
// get value any where in DrawAreaClass like this
Integer Value = MainActivity.getColorValue();
}
答案 2 :(得分:0)
在头等舱
intent i = new Intent(firstclass.this, secondclass.class).putextra("Key", "testing");
startActivity(i);
在第二课
String value = getIntent.getExtra("key");
答案 3 :(得分:0)
在两个需要使用意图的活动之间传递值
来源类:
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
的参考资料