启动电子邮件意图后,Android键盘仍然可见

wuf*_*foo 5 android android-intent

编辑:解决了.答案单独发布在下面

我正在启动内置的Intent.ACTION_SEND"选择器",以便用户可以选择如何从我的应用程序发送消息.它工作正常,但是如果我在启动的电子邮件程序中点击'Discard',它将返回到我的应用程序,屏幕键盘仍然可见.我试过用各种各样的imm.hideSoftInputFromWindow(...)关闭它,但无济于事.任何想法如何解决这一问题?

这就是我如何启动'选择器'并尝试在onActivityResult()中关闭键盘.请注意,tabHost是我的主应用程序(MainApp)中的静态成员,它包含用于创建tabSpecs的tabHost对象.

public class L_Secondary extends ListActivity implements myConst
{   
    @Override
   protected void onCreate (Bundle savedInstanceState)
   {
     super.onCreate (savedInstanceState);
     setContentView(R.layout.l_people_secondary);

     // instantiate the custom array adapter class and pass it some info to build a ListView with. 
     ListView lv = getListView ();
     lv.setOnItemClickListener (oicl);
     A_secondary da = new A_secondary (this, android.R.layout.simple_list_item_single_choice, mPiecesArray, mPartsArray);

     setListAdapter (da);
   }

   ...  


   // after launching the email client, the keyboard stays visible 
   // over the Listview. Currently the keyboard gets forced to close 
   // in getView() of the ArrayAdapter class da, in onCreate() above                
   public void launchEmail () 
   {
    try
    {
     // use the builtin chooser for users mail app
     Intent sendIntent = new Intent(Intent.ACTION_SEND, Uri.fromParts ("mailto", "root@localhost", null));
     sendIntent.setType("text/plain");    

     sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "msg_subject");
     sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, "msg_body");

     startActivityForResult (Intent.createChooser(sendIntent, "Send via which Application?"), 0);
    }
    catch (Exception e) 
    {
     Toast.makeText (this, "No activity was found to handle this action",Toast.LENGTH_SHORT).show();
    }
  }

 ...

}
Run Code Online (Sandbox Code Playgroud)

Lai*_*gem 5

通过将它添加到我的onResume(),我发现这对我有用

protected void onResume()
{
  Handler h = new Handler();
  h.postDelayed(new Runnable() {
     @Override
     public void run() {
        InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.hideSoftInputFromWindow(findViewById(android.R.id.content).getWindowToken(), 0);
     }
  }, 500);
}
Run Code Online (Sandbox Code Playgroud)


wuf*_*foo 0

我最终在 ArrayAdapter 类中使用传递给 getView() 的 Context,该类在 L_Secondary 类中实例化。这不是执行此操作的最佳位置,因为每次滚动、触摸或移动列表时,它都会检查键盘是否可见,如果可见则关闭它。尽管如此,这仍然是一个开始。从这里我可以尝试找到一个更有效的地方来放置它。

@Override
 public View getView (int position, View convertView, ViewGroup parent)
 {
     View row = convertView;
     Context ctx = parent.getContext ();

     if (row == null)
     {
         LayoutInflater inflater = ((Activity) ctx).getLayoutInflater ();
         row = inflater.inflate (R.layout.li_secondary, parent, false);
     }

     // hide the keyboard when coming back from Email client Intent
     InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
     if (imm.isActive () == true)
         imm.hideSoftInputFromWindow (MainApp.tabHost.getCurrentTabView ().getApplicationWindowToken (),imm.HIDE_NOT_ALWAYS);
     ...
}
Run Code Online (Sandbox Code Playgroud)