我如何判断输入法选择器是打开还是关闭? [英] How can I tell if the input method picker is open or closed?
问题描述
我的应用程序打开输入法选择器(可从中选择一个键盘菜单)与 InputMethodManager.showInputMethodPicker()
。我的应用程序不实际创建选择器(它是由InputMethodManager创建的),但我知道这是一个文本菜单
和它的id是 R.id.switchInputMethod
。
My app opens the input method picker (the menu where you choose a keyboard) with InputMethodManager.showInputMethodPicker()
. My app doesn't actually create the picker (it's created by InputMethodManager) but I know it's a ContextMenu
and its id is R.id.switchInputMethod
.
机械手是一个多步骤的向导,所以我需要知道什么时候该选择器关闭,所以我的应用程序可以继续进行下一步骤的一部分。现在我正在检查在后台线程,如果默认的键盘改变了,但是,如果用户选择了相同的键盘或presses回不帮忙。
The picker is part of a multi-step wizard so I need to know when the picker closes so my app can proceed to the next step. Right now I'm checking in a background thread if the default keyboard changed, but that doesn't help if the user selects the same keyboard or presses back.
所以,我需要一种方法来告诉当选择器关闭(或其他一些聪明的方式,知道什么时候进行)。
So I need a way to tell when the picker closes (or some other clever way to know when to proceed).
在此先感谢...
推荐答案
有对检查没有这样的机制 InputMethodPicker
是开放与否。
There is no such mechanism for check InputMethodPicker
is open or not.
但你可以通过另一种方式检查它像与使用 hasWindowFocus()
方法检查有根的布局重点。
But you can check it via another way like as using hasWindowFocus()
method for check has focus of your root layout.
下面是样本code:
的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btnPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Picker" />
<Button
android:id="@+id/btnCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
DemoappActivity.class
public class DemoappActivity extends Activity {
/** Called when the activity is first created. */
Button btn1 , btn2;
InputMethodManager imeManager;
LinearLayout mLayoutRoot;
TimerTask timertask;
Timer timer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLayoutRoot = (LinearLayout)findViewById(R.id.mainlayout);
imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
btn1 = (Button)findViewById(R.id.btnPicker);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showInputMethodPicker();
}
});
btn2 = (Button)findViewById(R.id.btnCheck);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
timer.cancel();
}
});
checkMyWindowHasFocus();
}
@Override
protected void onDestroy() {
timer.cancel();
super.onDestroy();
}
public void checkMyWindowHasFocus()
{
timertask = new TimerTask() {
@Override
public void run() {
System.out.println("has window focus..."+mLayoutRoot.hasWindowFocus());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Has focus "+mLayoutRoot.hasWindowFocus(), Toast.LENGTH_SHORT).show();
}
});
}
};
timer = new Timer();
timer.schedule(timertask, 500, 5000);
}
private void showInputMethodPicker() {
if (imeManager != null) {
imeManager.showInputMethodPicker();
} else {
Toast.makeText(this, "Error",Toast.LENGTH_LONG).show();
}
}
}
这篇关于我如何判断输入法选择器是打开还是关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!