[英]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.