gpt4 book ai didi

android - 实现我自己的 OnTouchListener

转载 作者:行者123 更新时间:2023-11-29 16:20:01 25 4
gpt4 key购买 nike

我想创建自己的 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 使用的每个组件。

最佳答案

以下对我有用。请检查并查看这是否有帮助。请随时修改构造函数以满足您的需要。对于此测试,我使用了一个线性布局,其中包含两个 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;
}
}

关于android - 实现我自己的 OnTouchListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7571644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com