从Email Activity返回后,Android键盘仍然可见

时间:2012-11-23 14:50:16

标签: android android-activity

我在TabHost中运行了一个Activity(扩展Activity)。我从用户操作启动Android电子邮件客户端。如果我点击电子邮件客户端中的“放弃”按钮,电子邮件客户端将退出,但屏幕上的键盘仍然可见。

我的应用程序上没有EditTexts,因此不确定为什么键盘会保持运行状态。我已经尝试了How do I remove the keyboard after I finish an activity?的几次迭代,但没有运气。有什么想法吗?

代码示例

package com.test.launchmail;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

public class myEmail extends Activity
{
    private final String TAG = "** Email **";


    public static void send (Context ctx, String addy, String subject, String body)
    {
        // check to make sure the entry has a phone number
        try
        {
            // use the builtin chooser for users mail app
            Intent sendIntent = new Intent(Intent.ACTION_SEND);
            sendIntent.setType("text/plain");

            sendIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String [] {addy});
            sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
            sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
            ctx.startActivity (Intent.createChooser(sendIntent, "Send via which Application?"));

        }
        catch (Exception e) 
        {
            Toast.makeText (ctx, "No activity was found to handle this action",Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void onPostResume()
    {
       // This executes, but keyboard still visible. 
        Log.d ("myEmail", "hiding");
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow (mainApp.tabHost.getCurrentTabView ().getApplicationWindowToken (),imm.HIDE_IMPLICIT_ONLY);

       super.onResume ();
    }
}

3 个答案:

答案 0 :(得分:2)

好悲伤多么刺激。我整天都在搜索,最后找到了以29种不同方式处理的答案。每个人都声称使用不同的InputMethodManager变体获得成功。对于它的价值,这个对我有用Close/hide the Android Soft Keyboard

希望有一天能够进行标准的API调用。

答案 1 :(得分:0)

尝试在所需的活动中放入您的清单文件(您不会显示键盘的活动):windowSoftInputMode="stateHidden"

答案 2 :(得分:0)

我的解决方案是在导航到电子邮件活动之前执行以下操作:

  1. 确保键盘隐藏在我的应用程序的屏幕上(使用流行的stackoverflow方法)
  2. ,最重要的是,确保屏幕上的EditText不再具有焦点。 contactSearchView.clearFocus()