2017-01-25 12 views
1

私は本当にこれで苦労してるので、私はあなたの良い人に向ける:)AutoCompleteTextView - プログラム的に設定された値と近いsoftkeyboard

目標:OnCreateの中にはAutoCompleteTextViewに値を設定し、キーボードを閉じます。

活動:2つのビュー、AutoCompleteTextViewと私はこれを呼び出すOnCreate関数のボタン

が含まれています。

private void SoftKeyboardClose() 
{ 
    IBinder windowToken; 
    AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.ac_city); 
    Button myBtn = (Button) findViewById(R.id.btn_search); 

    windowToken = myBtn.getWindowToken(); 

    if (windowToken == null) 
    { 
     windowToken = this.getWindow().getDecorView().getWindowToken(); 
    } 

    if (windowToken == null) 
    { 
     windowToken = findViewById(R.id.ac_city).getWindowToken(); 
    } 

    if (windowToken == null) 
    { 
     windowToken = new View(this).getWindowToken(); 
    } 


    // close it 
    InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

    if (inputManager == null) 
    { 
     Log.e(TAG, "inputmanage is null"); 
    } else if (windowToken == null) 
    { 
     Log.e(TAG, "getWindowToken is null"); 
    } else 
    { 
     inputManager.hideSoftInputFromWindow(windowToken, 0); 
    } 

    myBtn.requestFocus(); 
    if (myBtn.hasFocus()) 
    { 
     Log.e(TAG, "success - btn has focus"); 
    } else 
    { 
     Log.e(TAG, "fail - btn doesn't have focus"); 
    } 
} 

getWindowTokenは常にnullを返します。

hpedrorodriguesに対する応答で私はこれを試しました。

public class ZZZActivity extends Activity 
{ 
private static final String[] COUNTRIES = new String[]{"Belgium", "France", "Italy", "Germany", "Spain"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_zzz); 

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_zzz, COUNTRIES); 
    final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.myACTV); 
    textView.setAdapter(adapter); 
    closeKeyboard(this); 
} 


public void closeKeyboard(final Activity activity) 
{ 
    final View view = activity.getCurrentFocus(); 

    if (view != null) 
    { 
     final InputMethodManager manager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
     manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
    } 
} 

}

activity.getCurrentFocus()はnullが返さ。

+0

あなたは、ウィンドウのトークンを取得するために、これを試すことができます。理想的には、ウィンドウにビューが添付されている限り、有効なウィンドウトークンを持つ必要があります。 - 'getCurrentFocus()。getWindowToken()' – Dibzmania

+0

この場合、新しいビューを作成することができます。 –

答えて

1

おそらくフォーカスがAutoCompleteTextViewの代わりにボタンに表示されている可能性があります。

あなたがキーボードを閉じるには、このメソッドを使用することができます。

public void closeKeyboard(final Context context) { 
    View view = context.getCurrentFocus(); 

    if (view == null) { 
     view = new View(context); 
    } 

    final InputMethodManager manager = (InputMethodManager) 
     context.getSystemService(Context.INPUT_METHOD_SERVICE); 

    manager.hideSoftInputFromWindow(
     view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS 
    ); 
} 

コードを、私の提案を:

public class CountriesActivity extends Activity { 

    @Override 
    protected void onCreate(@Nullable final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.countries); 

     final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
      this, android.R.layout.simple_dropdown_item_1line, COUNTRIES); 

     final AutoCompleteTextView textView = (AutoCompleteTextView) 
      findViewById(R.id.countries_list); 

     textView.setAdapter(adapter); 

     YourUiUtil.closeKeyboard(this); 
    } 

    private static final String[] COUNTRIES = new String[] { 
     "Belgium", "France", "Italy", "Germany", "Spain" 
    }; 
} 

詳細はhereを見つけることができます。

+0

「android.R.layout.simple_dropdown_item_1line」とは何ですか? – ausgeorge

+0

activity.getCurrentFocus(); nullを返します – ausgeorge

+0

私のコードを更新しました –

0

この

public static void hideKeyboard(Context context) { 
     if(((Activity) context).getCurrentFocus()!=null){ 
      View view = ((Activity) context).getCurrentFocus(); 
      if (view != null) { 
       InputMethodManager inputManager = (InputMethodManager) context 
         .getSystemService(Context.INPUT_METHOD_SERVICE); 
       inputManager.hideSoftInputFromWindow(view.getWindowToken(), 
         InputMethodManager.HIDE_NOT_ALWAYS); 
      } 
     }else { 
      InputMethodManager inputManager = (InputMethodManager) context 
        .getSystemService(Context.INPUT_METHOD_SERVICE); 
      inputManager.hideSoftInputFromWindow(new View(context).getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
     } 
    } 

で試してみてくださいはこれを参照してください - https://stackoverflow.com/a/19083268/4741746

関連する問題