实现我自己的OnTouchListener

时间:2011-09-27 15:23:56

标签: android ontouchlistener

我想创建自己的OnTouchListener。然后我想将它封装到.jar文件中以使其可重用。

这是我特定的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();
    }

}

这就是我尝试使用它的方式:

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;
    }
}

我想让它适用于OnTouchListener使用的每个组件。

2 个答案:

答案 0 :(得分:4)

以下适用于我。请检查一下是否有帮助。请随意修改构造函数以满足您的需求。对于此测试,我使用了两个TextView(txtX,txtY)字段和一个GridLayout控件的线性布局。

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;
    }
}

答案 1 :(得分:0)

您已经创建了一个非常复杂的依赖关系网,您应该简化它。例如,您不应该像这样传递活动对象。

另外,在创建Activity类时,您不需要来重新定义构造函数。使用超级构造函数很好。您需要定义的是onCreate onStart onPause onStop onDestroy方法。我强烈建议您阅读Activity Documentation

比上面的实现更简单,就是摆脱myTouch界面。从implements OnTouchListener类中删除TouchableView,并在您的活动类中创建一个OnTouchListener类。

它看起来像这样:

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
    }
}