强制Android IME横向定位

时间:2015-12-04 01:52:20

标签: android ime

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

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

android:screenOrientation="landscape"

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

由于

1 个答案:

答案 0 :(得分:0)

正如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!
        }
    }
相关问题