2
我有2個活動的第一個活動使用renderview類不斷更改screencolor, 第二個活動使用設置爲contextview的textview顯示鼠標事件。 如何顯示mouseevents,同時我的背景不斷更改顏色。 我是新來的Andr ?? oid programming.please讓我知道如果我的問題是不清楚..在android編程中合併兩個視圖
代碼變化的背景:
public class renderviewtest extends Activity {
public class renderviewtest extends Activity {
class RenderView extends View {
Random rand = new Random();
public RenderView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
canvas.drawRGB(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
invalidate();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new RenderView(this));
}
}
Code for mouse events:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class singletouchtest extends Activity implements OnTouchListener {
StringBuilder builder = new StringBuilder();
TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Touch and drag (one finger only)!");
textView.setOnTouchListener(this);
setContentView(textView);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
builder.setLength(0);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
builder.append("down, ");
break;
case MotionEvent.ACTION_MOVE:
builder.append("move, ");
break;
case MotionEvent.ACTION_CANCEL:
builder.append("cancle, ");
break;
case MotionEvent.ACTION_UP:
builder.append("up, ");
break;
}
builder.append(event.getX());
builder.append(", ");
builder.append(event.getY());
String text = builder.toString();
textView.setText(text);
return true;
}
}