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

时间:2012-07-17 19:02:07

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

 ...

}

4 个答案:

答案 0 :(得分:5)

通过将其添加到我的onResume()

,我发现这对我有用
*ngFor

答案 1 :(得分:0)

在调用MAIL的意图之前使用此代码 // ed是EditText

InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);

隐藏键盘

imm.hideSoftInputFromWindow(ed.getWindowToken(), 0); 

用于显示键盘

imm.showSoftInput(ed, 0);

在onRestart()方法上尝试此代码

OR

你也可以试试这个

<activity android:name=".YourActivity"
          android:windowSoftInputMode="stateHidden"></activity>

感谢。

答案 2 :(得分:0)

我相信你可以在onResume()中调用hideSoftInputFromWindow方法

protected void onResume()
{
    InputMethodManager keyboard = (InputMethodManager)
    getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.hideSoftInputFromWindow(userInput.getWindowToken(), 0);
}

答案 3 :(得分: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);
     ...
}