2013-03-24 75 views
0

好吧,我正在做一个简单的秒表事物,当你按下屏幕上的任何地方时,我希望它开始时间,但现在,只需在输出中打印一些东西。我看了很多例子,我看了很多其他的答案,但都没有成功。这是我到目前为止(onTouch是在它的底部):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,这正是我试图测试的)

回答

1

我想你没有重写正确的事件。这就是为什么你的方法没有被调用。

您应该使用onTouchEvent代替,就像这样:

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

好吧,我没有你所说的,添加的覆盖,而新的回报,但它的错误,说:” ......必须重写或实现超类型方法“。不知道这是什么意思 – DXPower 2013-03-24 18:37:23

+0

你是否删除了你的“onTouch”方法。你应该只使用onTouchEvent – Aerilys 2013-03-24 18:40:43

+0

@DXPower:你需要在Activity中实现'OnTouchListener'监听器来覆盖活动 – 2013-03-24 18:45:04