4

我正在实现并将 an 归因onTouchListener于Textview ,但是当按下屏幕时运行时没有弹出消息。MainActivityOnTouchListenertv

主要活动

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

4 回答 4

2
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;
            }
        });
于 2015-01-06T09:47:36.190 回答
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;   
    } 
}
于 2012-10-06T05:19:45.253 回答
0

您必须 @override onTouch 方法,尝试删除实现 onTouchListener 并再次将其写入导入正确的方法,直到您编写它才会起作用

@Override
public boolean onTouch(View v,MotionEvent event)
{
    Toast.makeText(this,"onTouch",Toast.LENGTH_LONG).show();
    return true;
} 
于 2012-10-06T07:24:03.533 回答
0

嗯,onTouchListener() 有时只接收一些用户触摸输入。您能否将其设为 onClickListener() 并查看行为是否解决?

顺便说一句,我编译并运行了你的代码,它工作正常。你可以让 onTouch() 打印一条 Log.v() 消息并确保你的 onTouch() 被调用吗?

于 2012-10-06T00:23:08.563 回答