Android 当用户按下工具栏上的后退图标时,如何关闭键盘?

Android 当用户按下工具栏上的后退图标时,如何关闭键盘?,android,view,keyboard,listener,searchview,Android,View,Keyboard,Listener,Searchview,我试着用我的代码显示键盘,因为closeListener和onExpandListener不工作。我哪里失败了?当用户按下工具栏的后退按钮时,有一个方法可以调用吗 后退工具栏按钮的图像--> 这是我的java代码; 具有searchview的类: public class MainActivity extends ActionBarActivity { SearchView searchView = null; @Override protected void onCreate(Bundl

我试着用我的代码显示键盘,因为closeListener和onExpandListener不工作。我哪里失败了?当用户按下工具栏的后退按钮时,有一个方法可以调用吗

后退工具栏按钮的图像-->

这是我的java代码; 具有searchview的类:

public class MainActivity extends ActionBarActivity {

SearchView searchView = null;


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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    //Title and subtitle
    toolbar.setTitle("Magazzino Chimica");

    setSupportActionBar(toolbar);

    Intent intent = getIntent();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);

    // Associate searchable configuration with the SearchView
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));

    searchView.setIconifiedByDefault(false);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Toast.makeText(getApplicationContext(),"premuto",Toast.LENGTH_LONG).show();
    if (R.id.action_search == item.getItemId()){
        searchView.requestFocus();
        InputMethodManager tastiera = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        tastiera.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    return true;
}
我的searchable.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/label_ricerca"
    android:hint="@string/suggerimento_ricerca" >
</searchable>
编辑x2: 我尝试过,但只在单击2次后才传递到该方法,我将项目文件放在网上:),很抱歉使用xml,因为我是意大利人,xml资源中的所有写入都是意大利语,但只有4次写入:)

项目文件-->

编辑x3: 我听从了你的建议,但喷气机不起作用,你能试试我的代码吗?:)

在onCreateOptions菜单(菜单菜单菜单)中,添加以下行:

// This will add the back arrow to your action bar.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
在选项ItemSelected()中添加以下内容:

// Respond to the action bar's Up/Home button
if (item.getItemId() == android.R.id.home) {
    hideSoftKeyboard(this);
    // finish(), NavUtils.navigateUpFromSameTask(activity) or something like that
}
使用隐藏键盘的方法:

public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

覆盖活动的
onOptionsItemSelected
方法,以拦截点击
android.R.id.home

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        searchView.postDelayed(new Runnable() {

            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);`enter code here`
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
            }

        }, 300);

        return true;
    }
    return super.onOptionsItemSelected(item);
}

我已经编辑了我的答案。您需要使用我的答案中的方法将返回箭头添加到acion栏。然后,所有按下操作将自动发送到OnOptions ItemSelected()。您不应使用“NavigationOnClickListener”和“OnOptions ItemSelected”。删除侦听器,使用“OnOptions ItemSelected”一切都会正常工作
// Respond to the action bar's Up/Home button
if (item.getItemId() == android.R.id.home) {
    hideSoftKeyboard(this);
    // finish(), NavUtils.navigateUpFromSameTask(activity) or something like that
}
public static void hideSoftKeyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        searchView.postDelayed(new Runnable() {

            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);`enter code here`
                imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
            }

        }, 300);

        return true;
    }
    return super.onOptionsItemSelected(item);
}