Những ngày tuyệt vời.Thêm biểu tượng tìm kiếm trên thanh tác vụ android
Tôi có một biểu tượng tìm kiếm trong thanh hành động như hình ảnh dưới đây
Khi nó được nhấp, tôi muốn thay đổi thanh hành động để editText
và có một tìm kiếm icon
bên cạnh editText
tôi biết cách tạo một editText với một hình ảnh, nhưng làm thế nào để đặt editText trên thanh hành động như hình dưới đây? Và tôi muốn làm cho giá trị hiển thị dữ liệu sau khi editText chứa đầy giá trị. Tôi có nên sử dụng ý định không?
Đây là những gì tôi đã thử cho đến nay.
Hoạt động Một
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() {
//
}
}
Screen shot của Hoạt động Một
Khi biểu tượng tìm kiếm được nhấn, nó phải ý đến một trang khác và có một biểu tượng tìm kiếm trên editText (giống như hình ảnh thứ hai), nhưng không. Nó vẫn còn trong cùng một trang.
Đây là những gì tôi muốn (Từ WeChat)
Trước khi biểu tượng ép
Sau khi ép
Tôi nên sử dụng ý định khi biểu tượng tìm kiếm được nhấp để các dữ liệu liên quan có được hiển thị khi editText chứa đầy giá trị? – Hoo
Điều đó tùy thuộc vào mong đợi của bạn, với câu trả lời của tôi, bạn có thể sử dụng nó mà không sử dụng ý định. – Mohsen
android: biểu tượng = "@ android: drawable/ic_menu_search" – Jaydev