Dobrzy chłopaki dni.Dodaj ikonę wyszukiwania na pasku działań Android
mam ikonę wyszukiwania w pasku akcji jako obrazek poniżej
Po jego kliknięciu, chcę zmiany bar działanie do editText
i ma przeszukiwanie icon
obok editText
I wiedzieć, jak zrobić editText z obrazem, ale jak umieścić editText na pasku akcji jak na zdjęciu poniżej? I chcę ustawić wartość wyświetlania danych, gdy parametr editText zostanie wypełniony wartością. Czy powinienem użyć intencji?
To co próbowałem do tej pory.
aktywny Strzał
getData(deviceName, month); // retrieve data from `MySQL` and load into listView
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.create_menu, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
mSearchAction = menu.findItem(R.id.search);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.search: // should I need to add intent ?
handleMenuSearch();
return true;
case R.id.add: // create new file
View menuItemView = findViewById(R.id.add);
PopupMenu po = new PopupMenu(HomePage.this, menuItemView); //for drop-down menu
po.getMenuInflater().inflate(R.menu.popup_menu, po.getMenu());
po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
if ("Create New File".equals(item.getTitle())) {
Intent intent = new Intent(HomePage.this, Information.class); // go to Information class
startActivity(intent);
} else if ("Edit File".equals(item.getTitle())) {
Intent intent = new Intent(HomePage.this, Edit.class);
startActivity(intent);
}
return true;
}
});
po.show(); //showing popup menu
}
return super.onOptionsItemSelected(item);
}
protected void handleMenuSearch(){
ActionBar action = getSupportActionBar(); //get the actionbar
if(isSearchOpened){ //test if the search is open
action.setDisplayShowCustomEnabled(false); //disable a custom view inside the actionbar
action.setDisplayShowTitleEnabled(true); //show the title in the action bar
//hides the keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edtSeach.getWindowToken(), 0);
//add the search icon in the action bar
mSearchAction.setIcon(getResources().getDrawable(R.mipmap.search));
isSearchOpened = false;
} else { //open the search entry
action.setDisplayShowCustomEnabled(true); //enable it to display a
// custom view in the action bar.
action.setCustomView(R.layout.search_bar);//add the custom view
action.setDisplayShowTitleEnabled(false); //hide the title
edtSeach = (EditText)action.getCustomView().findViewById(R.id.edtSearch); //the text editor
//this is a listener to do a search when the user clicks on search button
edtSeach.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
doSearch();
return true;
}
return false;
}
});
edtSeach.requestFocus();
//open the keyboard focused in the edtSearch
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edtSeach, InputMethodManager.SHOW_IMPLICIT);
//add the close icon
mSearchAction.setIcon(getResources().getDrawable(R.mipmap.search));
isSearchOpened = true;
}
}
@Override
public void onBackPressed() {
if(isSearchOpened) {
handleMenuSearch();
return;
}
super.onBackPressed();
}
private void doSearch() {
//
}
}
Ekran działalności dla
Podczas wyszukiwania ikona jest wciśnięty, to ma zamiaru innej strony i mieć ikonę wyszukiwania na the editText (dokładnie jak drugi obraz), ale tak się nie stało. Pozostaje na tej samej stronie.
To jest to, co chcę (Z WeChat)
Przed ikona wciśnięty
Po naciśnięciu
Czy powinienem użyć intencji, gdy kliknięto ikonę wyszukiwania, aby powiązane dane pojawiły się, gdy editText wypełni się wartością? – Hoo
To zależy od oczekiwań, z moją odpowiedzią możesz to zrobić bez użycia intencji. – Mohsen
android: icon = "@ android: drawable/ic_menu_search" – Jaydev