【问题标题】:Force Android IME to landscape orientation强制 Android IME 为横向
【发布时间】:2016-03-08 20:54:45
【问题描述】:

我正在为 Android 制作一个替代键盘应用程序,我想强制此键盘进入永久横向模式(否则它看起来很糟糕)。

如果是活动,我知道该怎么做

android:screenOrientation="landscape"

当然,我的主类扩展了InputMethodService 而不是Activity,所以我不能使用它。用户界面是在 XML 文件上使用getLayoutInflater().inflate() 绘制的。

谢谢

【问题讨论】:

  • 横向模式是什么意思?您的意思是总是以提取模式出现(顶部带有大矩形编辑框的键盘在应用程序上全屏显示)?或者你的意思是强制整个活动进入横向模式(在这种情况下,答案是你不能,活动控制不是键盘服务)。
  • 为什么要让它成为一项服务?服务基本上是一个没有 UI 契约的活动(服​​务是一个应用程序组件,可以在后台执行长时间运行的操作,不提供用户界面)。你看过这里吗:developer.android.com/guide/topics/text/…
  • 你也可以看看另一个有很多活动的帖子。仅在您还没有走远的情况下才有帮助...stackoverflow.com/questions/9577304/…
  • 我实际上并没有使用提取模式,所以我没有编写onCreateExtractTextView 方法。你说得对,我的意思是强制(我猜有潜在的)活动进入横向模式。
  • 弗兰克,当我第一次创建这个想法时,我把它画成了一个活动。然而,为了让它作为键盘工作并将文本发送到其他应用程序,我不得不让它扩展InputMethodService。这使得它可以从设置中的可用键盘中进行选择,也可以用作标准键盘输入文本。作为一项活动,我怎样才能回到这样做?

标签: android ime


【解决方案1】:

正如 Gabe 所说,您无法控制应用的方向。

您可以做的是,如果应用处于纵向模式,则不显示键盘,而是显示一条消息“键盘仅在横向模式下工作”。

这可以使用onConfigurationChanged() 方法完成。

@Override
public void onConfigurationChanged(Configuration newConfig) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        IBinder token = getWindow().getWindow().getAttributes().token;

        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            imm.showSoftInputFromInputMethod(token, InputMethodManager.SHOW_FORCED);
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            imm.hideSoftInputFromInputMethod(token, InputMethodManager.RESULT_HIDDEN);
            Toast.makeText(getApplicationContext(),"Keyboard works only in landscape mode!",Toast.LENGTH_LONG).show();
        }
}

Ans 也在onStartInputView() 方法中进行检查:

@Override
    public void onStartInputView(EditorInfo info, boolean restarting) {
        super.onStartInputView(info, restarting);
        if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            //hide the keyboard & display a message (same as onConfigurationChanged())
        } else{
            //dont do anything...we are good to go!
        }
    }

【讨论】:

    猜你喜欢
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多