如何编写在 android 的操作栏中打开搜索时出现的后退箭头的功能?
How to write functionality for back arrow which appears when search is opened in action bar in android?
我在操作栏中有一个 collapseActionView 搜索。当我们按下搜索按钮时,后退箭头出现在操作栏中主页按钮的位置......在聚焦 edittext 软键盘时出现......当我们在不提交搜索值的情况下单击后退箭头软键盘不会隐藏自身...如何为打开搜索视图时出现在操作栏中的后退箭头编写功能?
@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);
}
}
我在操作栏中有一个 collapseActionView 搜索。当我们按下搜索按钮时,后退箭头出现在操作栏中主页按钮的位置......在聚焦 edittext 软键盘时出现......当我们在不提交搜索值的情况下单击后退箭头软键盘不会隐藏自身...如何为打开搜索视图时出现在操作栏中的后退箭头编写功能?
@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);
}
}