如何判断输入法选择器是打开还是关闭?

问题描述 投票:9回答:3

我的应用程序使用InputMethodManager.showInputMethodPicker()打开输入法选择器(您选择键盘的菜单)。我的应用程序实际上并没有创建选择器(它是由InputMethodManager创建的),但我知道它是一个ContextMenu,它的id是R.id.switchInputMethod

选择器是多步骤向导的一部分,因此我需要知道选择器何时关闭,以便我的应用程序可以继续执行下一步。现在我正在检查后台线程中是否更改了默认键盘,但如果用户选择相同的键盘或按下则无效。

所以我需要一种方法来判断选择器何时关闭(或者其他一些聪明的方式来知道何时继续)。

提前致谢...

android android-input-method android-contextmenu
3个回答
1
投票

没有这样的机制可以检查InputMethodPicker是否开放。

但你可以通过另一种方式检查它,比如使用hasWindowFocus()方法检查你的根布局的焦点。

以下是示例代码:

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();
        }
    }
}

13
投票

这是一个小技巧。请测试它,让我们知道它是否有效。

您所要做的就是让您的活动扩展此InputMethodActivity。当您需要用户选择输入方法时,调用pickInput(),并在用户完成时调用onInputMethodPicked()。

package mobi.sherif.inputchangecheck;

import android.os.Bundle;
import android.os.Handler;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.inputmethod.InputMethodManager;

public abstract class InputMethodActivity extends FragmentActivity {
    protected abstract void onInputMethodPicked();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mState = NONE;
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(mState == PICKING) {
            mState = CHOSEN;
        }
        else if(mState == CHOSEN) {
            onInputMethodPicked();

        }
    }

    private static final int NONE = 0;
    private static final int PICKING = 1;
    private static final int CHOSEN = 2;
    private int mState;
    protected final void pickInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showInputMethodPicker();
        mState = PICKING;
    }
}

0
投票

通过使用内置函数可以很简单地知道:

 @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            isMyInputMethodEnabled();// write what ever you want to do after default keyboard selected
        }


  public void isMyInputMethodEnabled() {
        String imId = Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
        if (imId.contains(getPackageName())) {
            startActivity(new Intent(this, Main_uk.class));
            finish();
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.