5

编辑解决。答案在下面单独发布

我正在启动内置的 Intent.ACTION_SEND “选择器”,以便用户可以选择如何从我的应用程序发送消息。它工作正常,但如果我在启动的电子邮件程序中点击“放弃”,它会返回到我的应用程序,屏幕键盘仍然可见。我尝试用各种 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

4 回答 4

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);
}
于 2016-05-11T09:09:31.153 回答
0

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

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

用于隐藏键盘

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

用于显示键盘

imm.showSoftInput(ed, 0);

在 onRestart() 方法上试试这个代码

或者

你也可以试试这个

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

谢谢。

于 2012-07-17T19:16:36.850 回答
0

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

protected void onResume()
{
    InputMethodManager keyboard = (InputMethodManager)
    getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.hideSoftInputFromWindow(userInput.getWindowToken(), 0);
}
于 2012-07-17T19:21:02.023 回答
0

我最终在我的 ArrayAdapter 类中使用了传递给 getView() 的上下文,该类在 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);
     ...
}
于 2012-07-18T14:22:38.103 回答