DispatchKeyEvent Android

时间:2015-03-11 12:51:14

标签: java android

当用户在我的应用程序中粘贴文本时,我会捕获键盘的键码,这个代码可以在Android模拟器中正常工作,但在实际设备中,toast不起作用:

public class MainActivity extends ActionBarActivity implements OnKeyListener {

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

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Toast.makeText(this, "Key dispatch : "+   String.valueOf(event.getKeyCode()),Toast.LENGTH_SHORT).show();
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        Toast.makeText(this, "Key onKey :" + String.valueOf(event.getKeyCode()), Toast.LENGTH_SHORT).show();
        return true;
    }
}

0 个答案:

没有答案