Android AutoCompleteTextView-通过编程设置值并关闭软键盘

Android AutoCompleteTextView-通过编程设置值并关闭软键盘,android,Android,我真的很挣扎,所以我转向你们这些好人:) 目标:在OnCreate期间,在AutoCompleteTextView中设置一个值并关闭键盘 活动:包含两个视图,AutoCompleteTextView和一个按钮 在OnCreate中,我称之为 private void SoftKeyboardClose() { IBinder windowToken; AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R

我真的很挣扎,所以我转向你们这些好人:)

目标:在OnCreate期间,在AutoCompleteTextView中设置一个值并关闭键盘

活动:包含两个视图,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);
    }
}
公共类zzzaActivity扩展活动
{
私有静态最终字符串[]国家=新字符串[]{“比利时”、“法国”、“意大利”、“德国”、“西班牙”};
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zzz);
最终阵列适配器=新阵列适配器(此,R.layout.activity_zzz,国家/地区);
最终自动完成文本视图文本视图=(自动完成文本视图)findViewById(R.id.myACTV);
setAdapter(适配器);
关闭键盘(这个);
}
公用键盘(最终活动)
{
最终视图=activity.getCurrentFocus();
如果(视图!=null)
{
最终InputMethodManager=(InputMethodManager)activity.getSystemService(Context.INPUT\u方法\u服务);
manager.hideSoftInputFromWindow(view.getWindowToken(),InputMethodManager.HIDE\u不总是);
}
}
}


activity.getCurrentFocus()返回null。

焦点可能在按钮上,而不是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"
    };
}
公共类CountriesActivity扩展活动{
@凌驾
创建时受保护的void(@Nullable final Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.countries);
最终ArrayAdapter适配器=新的ArrayAdapter(
这是android.R.layout.simple\u下拉列表\u item\u 1line,国家/地区);
最终自动完成文本视图文本视图=(自动完成文本视图)
findViewById(R.id.国家/地区列表);
setAdapter(适配器);
YourUiUtil.closeKeyboard(这个);
}
私有静态最终字符串[]国家/地区=新字符串[]{
“比利时”、“法国”、“意大利”、“德国”、“西班牙”
};
}
可以找到更多详细信息。

试试这个

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);
        }
    }

请参阅此-

您可以尝试此操作获取窗口令牌吗。理想情况下,只要视图连接到窗口,它就应该有一个有效的窗口标记-
getCurrentFocus()。getWindowToken()
您可以创建一个新视图。在这种情况下,请参阅我的更新。“android.R.layout.simple_dropdown\u item_1line”指的是什么?activity.getCurrentFocus();返回空值