Implementing my own OnTouchListener
I would like to create my own OnTouchListener. Then I would like to encapsulate it to a .jar file for making it reusable.
This is my specific OnTouchListener:
public class TouchableView extends View implements OnTouchListener{
myTouch t=null;
public TouchableView(Context context) {
super(context);
// Set KeyListener to ourself
this.setOnTouchListener(this);
}
public TouchableView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Set KeyListener to ourself
this.setOnTouchListener(this);
}
public TouchableView(Context context, AttributeSet attrs) {
super(context, attrs);
// Set KeyListener to ourself
this.setOnTouchListener(this);
}
public void setmyTouch(myTouch listener) {
t = listener;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
t.downTouch();
return true;
}
return false;
}
public interface myTouch{
public abstract boolean downTouch();
}
}
This is how I'm trying to use it:
public class MyTouchImplement extends Activity implements myTouch{
/** Called when the activity is first created. */
TextView tv;
int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.tv);
TouchableView view = (TouchableView) findViewById(R.id.view);
view.setmyTouch(this);
}
@Override
public boolean downTouch() {
i++;
tv.setText(i+"");
return true;
}
}
I would like to make it work for every component that t开发者_运维知识库he OnTouchListener works with.
The following works for me. Please check and see if this helps. Please feel free to modify the constructor to suit your needs. For this test I used a linear layout with two TextView (txtX, txtY) fields and one GridLayout control.
MineSweeperOnTouch.java
public class MineSweeperOnTouch implements View.OnTouchListener {
private View gridLayout = null;
private TextView txtX = null;
private TextView txtY = null;
public MineSweeperOnTouch(View aGridLayout, TextView aTxtX, TextView aTxtY) {
this.gridLayout = aGridLayout;
this.txtX = aTxtX;
this.txtY = aTxtY;
}
public boolean onTouch(View v, MotionEvent event) {
txtTimeX.setText("X: " + String.valueOf(event.getX()));
txtY.setText("Y: " + String.valueOf(event.getY()));
return true;
}
}
MainActivity.java (code snippet only)
-------------------------------------
public class MainActivity extends Activity {
private MineSweeperOnTouch gridLayoutListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//custom code starts here
final View gridLayout = findViewById(R.id.gridLayout);
final TextView txtX = (TextView) findViewById(R.id.txtX);
final TextView txtY = (TextView) findViewById(R.id.txtY);
gridLayoutListener = new MineSweeperOnTouch(gridLayout, txtX, txtY);
gridLayout.setOnTouchListener(gridLayoutListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You've created an awfully complicated web of dependencies that you should simplify. For example you shouldn't be passing around the activity object like that.
Also you when creating an Activity class you do not need to redefine the constructors. Using the super constructors is fine. What you do need to define are the onCreate
onStart
onPause
onStop
onDestroy
methods. I highly suggest you read the Activity Documentation
A simpler implementation than what you have above, would be to get rid of your myTouch interface. Remove the implements OnTouchListener
from the TouchableView
class and create a OnTouchListener
class inside your activity class.
It would look something like this:
public class MyTouchActivity extends Activity{
TouchableView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TouchableView();
tv.setOnTouchListener(new MyOwnTouchListener());
}
class MyOnTouchListener implements OnTouchListener{
public boolean onTouchEvent(View v, MotionEvent e){
switch(e.getAction){
case (MotionEvent.TOUCH_DOWN)
MyTouchActivity.this.touchDown();
break;
}
}
}
public boolean touchDown(){
//touch down happened
}
}
精彩评论