簡體   English   中英

強制將Android IME調整為橫向

[英]Force Android IME to landscape orientation

我正在為Android開發替代鍵盤應用程序,我想將此鍵盤強制設為永久橫向模式(否則看起來很糟糕)。

如果是一項活動,我知道該怎么做

android:screenOrientation="landscape"

但是我的主類當然擴展了InputMethodService而不是Activity所以我不能使用它。 使用XML文件上的getLayoutInflater().inflate()繪制用戶界面。

謝謝

正如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!
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM