触摸监听器上的Android imageview

时间:2013-12-16 19:09:14

标签: android imageview ontouchlistener

我试图在触摸侦听器上添加我的应用程序的imageview,但它无法正常工作。我尝试在创建和手势监听器上触摸侦听器,但无法弄明白。 当我点击图片时,我希望我的应用程序有新的意图。

public class MainActivity extends Activity {
private GestureDetectorCompat mDetector;
private Intent intent;
private MediaPlayer mMediaPlayer = null;
int length;
View img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ImageView view = (ImageView) findViewById(R.id.img1);
    /*view.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                intent = new Intent(MainActivity.this, MyListActivity.class);
                mMediaPlayer.pause();

                length=mMediaPlayer.getCurrentPosition();



                startActivity(intent);
                return true;
            }
       });*/
    // Hides the title bar (must be used before setContentView)
    // Requires import android.view.Window
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    //mDetector = new GestureDetectorCompat(this, new MyGestureListener());

    mMediaPlayer = MediaPlayer.create(this, R.raw.background);

    mMediaPlayer.start();
}

//@Override 
public boolean onTouchEvent(MotionEvent event){ 
    this.mDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}



class MyGestureListener extends SimpleOnGestureListener {
    @Override
    public boolean onDown(MotionEvent event) {

        return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {
        intent = new Intent(MainActivity.this, MyListActivity.class);
        mMediaPlayer.pause();

        length=mMediaPlayer.getCurrentPosition();



        startActivity(intent);
        return true;
    }
}

@Override
protected void onResume() {
    super.onResume();
    mMediaPlayer.seekTo(length);
    mMediaPlayer.start();
}

}

1 个答案:

答案 0 :(得分:1)

像这样设置onClickListener:

view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        intent = new Intent(this, MyListActivity.class);
        startActivity(intent);
    }
});

在setContentView()之后执行此操作。