gpt4 book ai didi

java - onTouch 无法正常工作

转载 作者:行者123 更新时间:2023-12-01 14:44:19 27 4
gpt4 key购买 nike

好吧,我正在制作一个简单的秒表,当您按屏幕上的任意位置时,我希望它开始计时,但现在只需在输出中打印一些内容。我看过很多例子,我看了很多其他问题的答案,但没有一个有效。这是我到目前为止所拥有的(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。这正是我试图测试的)

最佳答案

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

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

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

关于java - onTouch 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15602339/

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