打开/关闭软键盘 (Galaxy S)

发布于 2024-10-10 16:11:56 字数 3381 浏览 6 评论 0原文

好吧,又是一个奇怪的问题。我有活动 Main.java 和 DialogLikeActivity.java。

Main 中,我有

EditText (articleNumber) - phone input type (in .XML)
Button (articleOK)
list (list)

On DialogLikeActivity (活动,但使用对话框主题进行样式设置,并在清单中使用 android:windowSoftInputMode="stateAlwaysVisible" 标记)我有

EditText (articlePackaging) - phone input type (in .XML)
Button (finish)

我想要什么?当我输入主要活动键盘时,会使用电话输入类型打开,这很好。当我单击 EditText 字段时,填写一些内容并单击“确定”,静态变量变为状态 1(始终显示键盘) - 它只是主活动中的静态变量。

public static int keyboardMode = 1;

单击“确定”将打开 DialogLikeActivity,并打开键盘(电话输入类型)。当我单击“确定”时,主程序的“恢复”将启动,

@Override
public void onResume() {
    super.onResume();

    if (keyboardMode == 1) {
        showKeyboard(articleNumber);
    } else {
        hideKeyboard(articleNumber);
    }
}   

public static void showKeyboard(View view) {
    InputMethodManager mgr = (InputMethodManager) myContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);

    ((InputMethodManager) myContext.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(view, 0);

    articleNumber.setInputType(InputType.TYPE_CLASS_PHONE);
}

public static void hideKeyboard(View view) {
    InputMethodManager mgr = (InputMethodManager) myContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(view.getWindowToken(), 0);

    articleNumber.setInputType(0);
}

// Listener to showKeyboard if user clicks on EditText field and set InputType to PHONE, insted it would remain (0)

    articleNumber.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showKeyboard(articleNumber);
        }
    });

// Listener so when user touches the list - it hides the keyboard and sets variable on 2 so keyboard is always HIDDEN

    list.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            hideKeyboard(articleNumber);
            keyboardMode = 2;

            return false;
        }
    });

// Listener so when user clicks on ArticleOK - keyboard is always OPEN

articleOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {               
                keyboardMode = 1;

                    Intent i = new Intent(NewOrderArticles.this, NewQuantityDialog.class);
                    i.putExtra("articleid", articleId);
                    startActivity(i);
            }
        }
    });

并确保键盘以手机输入类型打开。如果我单击/滚动主键盘模式变量中的列表,则变为 2(始终隐藏) - 因此键盘会隐藏(使用名为 list.onTouch 侦听器的 hideKeyboard 方法)。如果我单击列表中的某些项目,它会再次打开 DialogLikeActivity 并打开键盘和手机输入类型。如果我在 Main 中单击“确定”,则会启动并隐藏键盘(请参阅上面的代码)。

看起来怎么样,请观看视频(视频中的“确定”按钮是文章“确定”按钮) - 视频 (3,24 MB)

所以实际上 - 如果我单击“确定”(文章确定)按钮 - 键盘始终必须显示,但是如果我开始滚动列表或单击列表中的某些项目 - 键盘必须显示仅在 DialogLikeActivity 活动 中,否则在 Main 中它必须保持隐藏状态(当然,如果我单击顶部的 EditText 字段,它会再次打开)。

在视频中,您可以看到 HTC Desire,一切都按照我想要的方式运行,但是 - 在 Galaxy S 上,始终显示部分正在运行,但如果我点击/滚动列表,它就会打开带有字母类型(!)的键盘而不是隐藏它。问题出在哪里?

所以“确定”按钮 - 始终显示键盘(在 Main 和 DialogLikeActivity 中)。如果点击/滚动列表仅在 DialogLikeActivity 中显示键盘。

Alright, another weird problem. I have activity Main.java and DialogLikeActivity.java.

In Main I have

EditText (articleNumber) - phone input type (in .XML)
Button (articleOK)
list (list)

On DialogLikeActivity (activity but styled with a Dialog theme and with android:windowSoftInputMode="stateAlwaysVisible" tag in Manifest) I have

EditText (articlePackaging) - phone input type (in .XML)
Button (finish)

What I want? When I enter Main activity keyboard is open with phone input type and that's fine. When I click on EditText field, fill in something and click OK static varible becomes in state 1 (always show keyboard) - it's just static variable in Main activity.

public static int keyboardMode = 1;

Clicking on OK opens up DialogLikeActivity with keyboard open (phone input type). When I click OK onResume of Main kicks in

@Override
public void onResume() {
    super.onResume();

    if (keyboardMode == 1) {
        showKeyboard(articleNumber);
    } else {
        hideKeyboard(articleNumber);
    }
}   

public static void showKeyboard(View view) {
    InputMethodManager mgr = (InputMethodManager) myContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);

    ((InputMethodManager) myContext.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(view, 0);

    articleNumber.setInputType(InputType.TYPE_CLASS_PHONE);
}

public static void hideKeyboard(View view) {
    InputMethodManager mgr = (InputMethodManager) myContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(view.getWindowToken(), 0);

    articleNumber.setInputType(0);
}

// Listener to showKeyboard if user clicks on EditText field and set InputType to PHONE, insted it would remain (0)

    articleNumber.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showKeyboard(articleNumber);
        }
    });

// Listener so when user touches the list - it hides the keyboard and sets variable on 2 so keyboard is always HIDDEN

    list.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            hideKeyboard(articleNumber);
            keyboardMode = 2;

            return false;
        }
    });

// Listener so when user clicks on ArticleOK - keyboard is always OPEN

articleOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {               
                keyboardMode = 1;

                    Intent i = new Intent(NewOrderArticles.this, NewQuantityDialog.class);
                    i.putExtra("articleid", articleId);
                    startActivity(i);
            }
        }
    });

And ensures that keyboard is open with phone input type. If I howewer click/scroll through the list in Main keyboardMode variable becomes 2 (always hide) - so the keyboard hides (with hideKeyboard method called list.onTouch listener). If I click on some item in the list it again opens DialogLikeActivity with open keyboard and phone input type. If I click OK onResume in Main kicks in and hides keyboard (see code upthere).

How does that look like please see video (OK button in video is articleOK button) - Video (3,24 MB).

So actually - if I click OK (articleOK) button - keyboard always must be shown, if I, however start scrolling thorugh the list or click on some item in the list - keyboard must be shown only in DialogLikeActivity activity, otherwise in Main it must stay hidden (ofcourse if I click on EditText field on the top it opens up again).

On video you can see HTC Desire and everything works just the way I want, but - on Galaxy S the always show part is working, but if I tap/scroll the list, it open's up keyboard with letter type (!) instead of hiding it. Where's the problem?

So OK button - always show keyboard (in Main and DialogLikeActivity). If tap/scroll the list show keyboard only in DialogLikeActivity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅听莫相离 2024-10-17 16:11:56

我对 EditText 也有同样的问题,它会弹出带有数字的键盘。在 Samsung Galaxy S 设备上,单击“确定”或“取消”后,虚拟键盘不会隐藏,而是显示字母类型的键盘。
它在模拟器 2.1 或 2.2 以及我的 HTC Hero 上运行良好,但在 2.2 的 Samsung Galaxy S 设备上运行不佳。
我也尝试过:
mgr.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
但仍然没有机会

编辑:好的,问题仅用这一行解决:

mgr.toggleSoftInput(0, 0);

并且没有这两行:

mgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
articleNumber.setInputType(0);

I have the same problem with an EditText that pops up the keyboard with numbers. After clicking on OK or Cancel, the virtual keyboard does not hide and shows keyboard with letter type, on the Samsung Galaxy S device.
It's working fine on Emulators 2.1 or 2.2 and my HTC Hero, but not on Samsung Galaxy S device with 2.2.
I also tried:
mgr.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
but still no chance

EDIT: ok, problem solved with this line only:

mgr.toggleSoftInput(0, 0);

and without these 2 lines:

mgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
articleNumber.setInputType(0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文