我有一個小程序,我有2個圖像,現在我正在爲每個圖像做點擊時,頂部的圖像設置爲不可見的OnTouch事件。這工作正常..但在主要活動有這越來越雜亂..Android的一般問題
我創建了一個TestView,我想將我在主要活動中的代碼移動到testView,但我不知道在哪裏把OnTouch事件和如何做到這一點?,我不知道在哪裏把OnTouch事件,以及如何從我的testView繪製ImageViews ..當我在主要活動,他們畫得很好..但現在..我不知道如何從「testView延伸視圖」類中繪製它們...你能幫我組織它嗎?
public class TestView extends View{
public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public TestView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public TestView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
}
public class GameActivity extends Activity {
/** Called when the activity is first created. */
public int ColorId;
public boolean isUserTurn = false;
public int globalIndex = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myviewlay);
final ImageView image1= (ImageView)findViewById(R.id.ImageView03);
image1.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(200);
ColorId = 1;
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN){
image1.setVisibility(View.INVISIBLE);
}
else
{
image1.setVisibility(View.VISIBLE);
}
//return super.onTouchEvent(event);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
});
final ImageView image2= (ImageView)findViewById(R.id.ImageView02);
image2.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(200);
ColorId = 2;
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN){
image2.setVisibility(View.INVISIBLE);
}
else
{
image2.setVisibility(View.VISIBLE);
}
//return super.onTouchEvent(event);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
});
我只是想知道,如果我把它移動到測試的看法,我怎麼可以重新組織,以便它從那裏工作。我很新到Android ...
謝謝
謝謝你,這是不明確的唯一的事情是我在哪裏把我ImageViews與OnTouch功能我在我的主要活動...? :( – user710502 2011-04-21 03:04:37
你可以用它們創建一個XML佈局,只要記住在佈局中使用自定義視圖時必須提供整個類路徑(例如)。你的活動只是調用'setContentView()'我早些時候說過:) –
JQCorreia
2011-04-21 15:32:54