【问题标题】:onTouch Not working properly [duplicate]onTouch 无法正常工作 [重复]
【发布时间】:2013-03-24 18:30:34
【问题描述】:

好的,我正在制作一个简单的秒表,当您按下屏幕上的任意位置时,我希望它开始计时,但现在,只需在输出中打印一些内容即可。我看过很多例子,我看了很多其他问题的答案,但没有一个有效。这是我到目前为止所拥有的(onTouch 在它的底部):

package com.timeofcubeeliteDYLANFERRIS.cubetimerelite;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
final private int RANDOM_DIALOG = 0;
String message;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@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, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId())
    {
    case R.id.settingsMenu: 
        Intent intent = new Intent (MainActivity.this, MainPreferenceActivity.class);
        startActivity(intent);
        break;
    case R.id.cubetypeMenu:
        Intent cubeTypeIntent = new Intent (MainActivity.this, PreferenceCubeType.class);
        startActivity(cubeTypeIntent);


        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        String value = preferences.getString("test", "String not found");
        System.out.println(value);
        Log.d("Cubetimer:", value);
        break;
    default:
    }
    return super.onOptionsItemSelected(item);
}






public boolean onTouch(View view, MotionEvent event){
    int action = event.getAction();
    switch(action){
    case MotionEvent.ACTION_DOWN: Log.d(message, "down"); break;
    case MotionEvent.ACTION_MOVE: Log.d(message, "move"); break;
    case MotionEvent.ACTION_UP: Log.d(message, "up"); break;

    }
    System.out.println("Oh my oh MY...");
    return true;
}

}

那么,我将如何修复触摸事件? (我不在乎保留 ACTION_UP、DOWN、MOVE。这正是我想要测试的)

【问题讨论】:

    标签: java android ontouchlistener


    【解决方案1】:

    我认为您没有覆盖正确的事件。这就是你的方法没有被调用的原因。

    您应该改用onTouchEvent,如下所示:

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        //Do your stuff
        return super.onTouchEvent(event);
    }
    

    【讨论】:

    • 好的,我按照你说的做了,添加了覆盖和新的返回,但是它出错了,说“......必须覆盖或实现超类型方法”。不知道这是什么意思
    • 您是否删除了“onTouch”方法。你应该只使用 onTouchEvent
    • @DXPower :您需要在 Activity 中实现 OnTouchListener Listener 以覆盖 Activity 中的 onTouch 方法
    • 修复了它,我没有看到那里的变化。多谢兄弟。 :D
    猜你喜欢
    • 2017-07-15
    • 2015-09-06
    • 2016-05-06
    • 2016-05-12
    • 2018-07-19
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多