android - 如何编写在 android 的操作栏中打开搜索时出现的后退箭头的功能?

标签 android android-actionbar

我在操作栏中有一个 collapseActionView 搜索。当我们按下搜索按钮时,后退箭头出现在操作栏中主页按钮的位置......在聚焦 edittext 软键盘时出现......当我们点击时我遇到了问题没有提交搜索值的后退箭头软键盘不会隐藏自己...如何为打开搜索 View 时出现在操作栏中的后退箭头编写功能?

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {

            inflater.inflate(R.menu.main_activity_bar_l, menu);
        } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {

            inflater.inflate(R.menu.main_activity_bar, menu);
        } else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {

            inflater.inflate(R.menu.main_activity_bar, menu);
        } else {

            inflater.inflate(R.menu.main_activity_bar, menu);
        }
        View v = (View) menu.findItem(R.id.my_action).getActionView();




        /** Get the edit text from the action view */
        txtSearch = (EditText) v.findViewById(R.id.myActionEditText);

        /** Setting an action listener */

        ImageView Search = (ImageView) v.findViewById(R.id.search);
        Search.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                search_value = txtSearch.getText().toString();
                if (txtSearch.getText().toString().equals("")) {
                    Toast.makeText(getBaseContext(),
                            "Please enter search values", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    Intent intent = new Intent(MainActivity.this,
                            Display.class);
                    intent.putExtra("Action_search", txtSearch.getText()
                            .toString());
                    startActivity(intent);
                    txtSearch.setText("");
                }
            }

        });

        return super.onCreateOptionsMenu(menu);
    }

我为主页按钮编写了功能,但它不起作用

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // toggle nav drawer on selecting action bar app icon/title
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        // Handle action bar actions click
         switch (item.getItemId()) {
//      
//       
//       
    case android.R.id.home:  

        Toast.makeText(MainActivity.this, "home", Toast.LENGTH_LONG).show();
         // count the active fragment
//       if(getSupportFragmentManager().getStackBackEntryCount() > 0) {
//           // hide soft method as above
//           InputMethodManager mImm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
//           mImm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
//           // do the pop backstack
//           getSupportFragmentManager().popBackStack(); 
//       } else {  
//           // some stuff like finish the activity
         }
        return super.onOptionsItemSelected(item);
    }

最佳答案

在onPause中写这段代码即可:

public void hideSoftKeyboard() {
if(getCurrentFocus()!=null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}

关于android - 如何编写在 android 的操作栏中打开搜索时出现的后退箭头的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207152/

相关文章:

android - AppCompat v7 :21 Split Action Bar Broken?

Android actionBar OnClick()

android - 在动态线性布局中设置 Imageview 的大小

Android aar 依赖项

java - viewmodelfactory 和使用 Activity 模块的 View 模型注入(inject)之间的区别

android - 从小部件获取 SCREEN_ON 和 SCREEN_OFF Intent

android - 操作栏上的下拉菜单

android - 带有 ActionBarSherlock 的原生 ActionBar selectableItemBackground

操作栏中的 Android 搜索按钮和搜索字段

android - 铃声选择器 : check 'none' or 'default notification sound' when they're picked