gpt4 book ai didi

java - 上下文语音命令后失去 Activity 焦点

转载 作者:行者123 更新时间:2023-12-01 22:54:19 24 4
gpt4 key购买 nike

我正在尝试在 documentation 中描述的 Glass 应用程序中实现上下文语音命令。 .

我的布局中有一个 FrameLayout,在我的 Activity 中,我正在实现 OnKeyDown 来识别点击。但是,当用户说“Ok glass”然后选择可用命令之一时,会返回相同的 Activity ,但没有响应(我什至无法通过向下滑动来关闭应用程序)。

我尝试以几种不同的方式在 FrameLayout 上使用 requestFocus(),但没有任何效果。如何在不重新启动 Activity 的情况下重新获得焦点?

本质上,这是我迄今为止主要 Activity 的代码:

public class MainActivity extends Activity {

public static String TAG = "DummyProject::MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);

setContentView(R.layout.activity_main);
}

@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
getMenuInflater().inflate(R.layout.main, menu);
return true;
}

return super.onCreatePanelMenu(featureId, menu);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.main, menu);
return true;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
if (featureId == WindowUtils.FEATURE_VOICE_COMMANDS) {
switch (item.getItemId()) {
case R.id.awesome:
Log.i(TAG, "Selected!");
break;
default:
return true;
}

return true;
}

return super.onMenuItemSelected(featureId, item);
}

@Override
public boolean onKeyDown(int keycode, KeyEvent event) {
if(keycode == KeyEvent.KEYCODE_DPAD_CENTER) {
Log.i(TAG, "tap!");
return true;
}

return super.onKeyDown(keycode, event);
}
}

对于此示例,在我选择任何“ok glass”菜单选项后,不会调用 onKeyDown。

提前谢谢您。

最佳答案

更喜欢 GestureDetector 来捕获触摸板事件...

...

private GestureDetector mGestureDetector;

...

@Override
protected void onCreate(Bundle savedInstanceState) {

...

mGestureDetector = createGestureDetector(this);

...

}

...

private GestureDetector createGestureDetector(Context context) {

GestureDetector gestureDetector = new GestureDetector(context);
// Create a base listener for generic gestures
gestureDetector.setBaseListener(new GestureDetector.BaseListener() {
@Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) {
Log.d(TAG, "TAP!!!");
return true;
}
return false; }
});

gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
@Override
public void onFingerCountChanged(int previousCount, int currentCount) {
// do something on finger count changes
}
});

gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
@Override
public boolean onScroll(float displacement, float delta, float velocity) {
// do something on scrolling
return true;
}
});
return gestureDetector;
}

/*
* Send generic motion events to the gesture detector
*/
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}

关于java - 上下文语音命令后失去 Activity 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24316485/

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