它的接口是用来检测从对话框类(Android SDK中)关键事件? [英] Which interface is used to detect key events from the Dialog class (Android SDK)?
问题描述
我已经创建了一个要求用户preSS的关键一个简单的自定义对话框。这样做的目的是为了让我能够映射无论他们的关键preSS在应用程序的功能。不幸的是,我无法弄清楚什么是用来检测的关键事件正确的接口。我的阶级是这样的:
I've created a simple custom dialog that asks users to "Press a key". The purpose of this is so that I can map whatever key they press to a function in the app. Unfortunately, I can not figure out what is the correct interface to use to detect the key events. My class looks like this:
public class ScancodeDialog extends Dialog implements OnKeyListener
{
public ScancodeDialog( Context context )
{
super(context);
setContentView( R.layout.scancode_dialog );
setTitle( "Key Listener" );
TextView text = (TextView) findViewById( R.id.scancode_text );
text.setText( "Please press a button.." );
ImageView image = (ImageView) findViewById( R.id.scancode_image );
image.setImageResource( R.drawable.icon );
getWindow().setFlags( WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM );
}
@Override
public boolean onKey( DialogInterface dialog, int keyCode, KeyEvent event )
{
if( keyCode != KeyEvent.KEYCODE_MENU )
dismiss();
return true;
}
}
我一直有和没有getWindow()尝试过。setFlags()行(这是另一个问题,它并没有帮助我在我的情况建议)。很显然,我会添加更多的功能到后来类,但现在每当用户presses一个关键的对话框应该关闭。然而,安其不会被调用。
I've tried it with and without the getWindow().setFlags() line (that was a suggestion from another question, which didn't help me in my case). Obviously I will add more functionality to the class later, but for now the dialog box should close whenever the user presses a key. However, onKey is never called.
我使用的关键监听器接口从View最初尝试:
I originally tried using the key listener interface from View:
import android.view.View.OnKeyListener;
但由于一个对话框是不是认为,这并不工作。我也尝试从DialogInterface之一:
But since a Dialog is not a view, this didn't work. I also tried the one from DialogInterface:
import android.content.DialogInterface.OnKeyListener;
这似乎是一个更好的选择,因为API表明对话实现DialogInterface,但我仍然没有收到关键事件。任何建议我可以尝试?
This seemed like a better choice, since the API indicates that Dialog implements DialogInterface, but I am still not receiving the key events. Any suggestions I can try?
推荐答案
您从未声明过对话听键。
You never declared the dialog to listen to the keys.
例如:
this.setOnKeyListener(...)
this关键字是指类,它是在..这是一个对话框。
the this keyword is referring to the class that it is in.. which is a Dialog.
这篇关于它的接口是用来检测从对话框类(Android SDK中)关键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!