2012-07-17 43 views
5

編輯已解決。下面單獨發佈答案Android鍵盤啓動後仍然可見電子郵件意圖

我正在啓動內置的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", "[email protected]", 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(); 
    } 
    } 

... 

} 

回答

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); 
    ... 
} 
+0

這是爲什麼這個工作?我需要了解並嘗試在我的情況下使用它。 – Poutrathor 2013-11-21 17:15:05

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> 

感謝。

+0

我會嘗試,但我沒有一個EditText在我的ListView。我會試驗謝謝你。 – wufoo 2012-07-17 19:25:39

+0

如果您在listActivity中沒有任何編輯文本,我已經編輯了一個更多解決方案的代碼。謝謝 – SALMAN 2012-07-17 19:36:38

+0

謝謝。我有stateHidden集,但仍然沒有運氣。 – wufoo 2012-07-17 20:14:09

0

我相信你可以調用的onResume()的方法hideSoftInputFromWindow

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

這是爲什麼downvoted? – Leon 2014-07-23 10:54:49

+0

不知道,但如果我做錯了什麼,請讓我知道 – 2014-08-12 02:57:11

+0

什麼是userInput? – 2016-09-07 10:22:59

2

我發現這個工作對我來說將它添加到我的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); 
} 
+0

唯一有效的東西... – peresisUser 2017-02-27 10:09:40

+0

你也可以在onActivityResult()而不是onResume()中寫這個。 – 2017-09-14 09:08:52

相關問題