android - 当用户按下工具栏中的后退图标时,我如何关闭键盘?

标签 android view keyboard listener searchview

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

返回工具栏按钮的图像 --> http://imgur.com/WhzT1tp

这是我的java代码;
具有搜索 View 的类:

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>

我的 list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.developer.eliax1996.databasegestionariochimica" >

    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultsActivity" />

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
    </activity>

    <!-- Search results activity -->
    <activity android:name=".SearchResultsActivity"
        android:parentActivityName=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

    </application>

</manifest>

和我的菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">

    <item
    android:id="@+id/action_search"
    android:title="cerca"
    android:icon="@drawable/ic_search_white"
    app:showAsAction="collapseActionView|ifRoom"
    app:actionViewClass="android.widget.SearchView" />

</menu>

当用户按下工具栏中的后退按钮时,我如何关闭键盘?

编辑:
我试过了,但是当应用程序运行时,如果我按下图像指示的按钮,程序不会传入这个方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {}

编辑 x2:
我试过了,但它只有在点击 2 次后才传递给该方法,我将项目文件放到网上 :),对于 xml 感到抱歉,因为我是意大利人,xml 资源中的所有写入都是意大利语,但只有 4 次写入 :)

项目文件 --> https://drive.google.com/file/d/0B907WWO7eA5bZ1BTUmlIekxDaGs/view?usp=sharing

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

最佳答案

在 onCreateOptionsMenu(Menu menu) 中,添加这一行:

// This will add the back arrow to your action bar.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

在您的 onOptionsItemSelected() ,添加这个:
// 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);
}

关于android - 当用户按下工具栏中的后退图标时,我如何关闭键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27929421/

相关文章:

Android:有没有办法创建隐藏文件夹和隐藏文件?

Android:如何将 View 添加到 WindowManager,并使其始终 float 在我的应用程序顶部?

javascript - Backbone.js — 匿名 View 实例中的集合在获取时多次请求

swift - 仅在 TestFlight 版本和 IOS 14 中显示键盘后应用程序崩溃

android - MPAndroidChart - 如何在另一个位置设置描述?

android - 如何清除 fragment 后台直到指定 fragment ?

android - 使用 Android 从摄像机捕获 WiFi 信号

Android View 和 View 组

android - 自动展开编辑文本

Android:尝试用 View 替换键盘时如何防止 View 跳转/闪烁