我正在onTouchListener
中实施MainActivity
并将OnTouchListener
归因于Textview tv
,但是在按下屏幕时不会弹出任何消息。
MainActivity
public class MainActivity extends Activity implements View.OnTouchListener
{
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
tv.setText(R.string.hello);
}
public boolean onTouch(View v,MotionEvent event)
{
Toast.makeText(this,"onTouch",Toast.LENGTH_LONG).show();
return true;
}
}
答案 0 :(得分:1)
如果同意
,请尝试接受 tv.setOnTouchListener(new CustomTouchListener());
public class CustomTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
switch(motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
// Action you you want on finger down.
Toast.makeText(this,"onTouch",Toast.LENGTH_LONG).show();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// Action you you want on finger up
break;
}
return false;
}
}
答案 1 :(得分:1)
TextView lastTV= (TextView) findViewById(R.id.lastTvValue);
lastTV.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//do stuff here
}
Log.i("click text", "kakak");
return false;
}
});
答案 2 :(得分:0)
嗯,onTouchListener()有时只接收一些用户触摸输入。你能把它变成一个onClickListener()并看看行为是否解决了?
我编译并运行了你的代码,顺便说一句,它运行正常。你有onTouch()打印一个Log.v()消息,并确保你的onTouch()被调用?
答案 3 :(得分:0)
你必须@override onTouch方法,尝试删除实现onTouchListener并再次写入导入正确的一个,直到你写
它才会工作@Override
public boolean onTouch(View v,MotionEvent event)
{
Toast.makeText(this,"onTouch",Toast.LENGTH_LONG).show();
return true;
}