如何从自定义视图中引用同一活动中的按钮

时间:2015-02-15 01:02:14

标签: java android

我正在尝试制作一个简单的绘图应用程序,但是在切换绘制颜色方面遇到了问题。Here是我用来达到我所处的目的的教程。这是我的代码: 的 Drawing_View.java

public class Drawing_View extends View {
private Path path = new Path();
private Paint paint = new Paint();
public String paint_color = "#FFBB00";

public Drawing_View(Context context, AttributeSet attrs) {
    super(context, attrs);
    paint.setAntiAlias(true);
    paint.setStrokeWidth(5f);
    paint.setColor(Color.parseColor(paint_color));
    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 eventY = event.getY();
    float eventX = event.getX();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Set a new starting point
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
            // Connect the points
            path.lineTo(eventX, eventY);
            break;
        default:
            return false;
    }

    invalidate();
    return true;
}
}

以下是自定义视图所在活动的代码。 的 Drawing_Main.java

public class Drawing_Main extends ActionBarActivity {
Button green_light,green_dark,blue_light,blue_dark,blue_verydark,purple,magenta,red,orange_dark,orange_light,orange_lightest,yellow,black;
String btn_color_hex;
SharedPreferences.Editor editor = getSharedPreferences("paint_color", MODE_PRIVATE).edit();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_emoji_creation);
    green_light = (Button)findViewById(R.id.button1);
    green_dark = (Button)findViewById(R.id.button2);
    blue_light = (Button)findViewById(R.id.button3);
    blue_dark = (Button)findViewById(R.id.button4);
    blue_verydark = (Button)findViewById(R.id.button5);
    purple = (Button)findViewById(R.id.button6);
    magenta = (Button)findViewById(R.id.button7);
    red = (Button)findViewById(R.id.button8);
    orange_dark = (Button)findViewById(R.id.button9);
    orange_light = (Button)findViewById(R.id.button10);
    orange_lightest = (Button)findViewById(R.id.button11);
    yellow = (Button)findViewById(R.id.button12);
    black = (Button)findViewById(R.id.button13);
    green_light.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            btn_color_hex="#0fad00";
            editor.putString("paint_color_hex", btn_color_hex);
            editor.commit();


        }
    });
    green_dark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    blue_light.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }

    });
    blue_dark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    blue_verydark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    purple.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    magenta.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    red.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    orange_light.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    orange_lightest.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    orange_dark.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    yellow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    black.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_emojicreation, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

正如您所看到的,在我尝试创建服务失败后,我开始探索sharedPreferences选项。如果服务是最佳选择,我怎样才能使Drawing_View.java不断监听颜色变化?谢谢大家!

1 个答案:

答案 0 :(得分:1)

您可以在setPaintColor课程中添加Drawing_View方法。这样,您就不需要使用SharedPreferences 你可以改变onClickListener中的油漆颜色。

这样的事情应该有效:

Drawing_View课程中,添加:

public void setPaintColor (String color) {
   paint.setColor(Color.parseColor(color));
}

onClickListener中,调用setPaintColor方法:

drawingView.setPaintColor(color);

我假设你的Drawing_View已经实例化了。

<强>更新

您需要以与活动的Drawing_View方法中Button实例化相同的方式实例化onCreate

public class Drawing_Main extends ActionBarActivity {

...

Drawing_View drawingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_emoji_creation);
    drawingView = (Drawing_View)findViewById(R.id.drawing_view);
    green_light = (Button)findViewById(R.id.button1);
    ...    

我希望这会有所帮助。