安卓:检测softkeyboard RTL VS LTR(语文方向) [英] Android: Detect softkeyboard RTL Vs LTR (language direction)

查看:237
本文介绍了安卓:检测softkeyboard RTL VS LTR(语文方向)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着<一个href=\"http://stackoverflow.com/questions/18008189/how-to-get-text-direction-in-android-and-change-layout-dynamically-according-to\">this看来,检测语言方向的方法是使用一些Java库的就已经键入的文本

Following this it seems that the way to detect language direction is to use some Java libs on the already typed text.

我有一个AutoCompleteTextView,所以我希望在软键盘弹出,就知道所选择的语言(在用户键入文本之前)。因此,当用户在键盘语言切换,我可以知道,如果选择的语言是RTL或LTR(因此相应地改变布局)。例如大多数与本地RTL语言的用户有键盘,他们的语言和英语。

I have an AutoCompleteTextView so I want when the soft keyboard pops-up to know the selected language (before the user typing text). So when the user switches on the keyboard language I can know if the selected language is RTL or LTR (and therefore change layout accordingly). For example most of the users with native RTL language have keyboard with their language and English.

谢谢,

推荐答案

要检测键盘的语言此刻的EditText获得焦点时,您可以使用下面的代码片段

To detect the language of the keyboard at the moment the EditText gains focus, you can use the following snippet

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText editText = (EditText) findViewById(R.id.et);
        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    String language = getKeyboardLanguage();
                    Log.d("language is", language);
                }
            }
        });
    }

    private String getKeyboardLanguage() {
        InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        InputMethodSubtype inputMethodSubtype = inputMethodManager.getCurrentInputMethodSubtype();
        return inputMethodSubtype.getLocale();
    }
}

起初,我的键盘语言是荷兰语和它印

At first, my keyboard language was Dutch, and it printed

D/language is: nl

然后,我改变了我的键盘语言为英语/美国与它印

then, I changed my keyboard language to English/United Stated and it printed

D/language is: en_US

要检测的语言是否RTL或LTR,您可以使用此

To detect whether the language is RTL or LTR, you could use this

private boolean isRTL(Locale locale) {
    final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
    return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
           directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
}

<子>这isRTL()方法,我发现在<一个href=\"http://stackoverflow.com/a/23203698/1843331\">http://stackoverflow.com/a/23203698/1843331

调用从 String语言创建一个区域设置:

Call that with a Locale created from the String language:

String language = getKeyboardLanguage();
boolean isRTL = isRTL(new Locale(language));

这篇关于安卓:检测softkeyboard RTL VS LTR(语文方向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆