我必须在下面的类中添加更多代码,但我需要在另一个类中分隔handleMenuSearch()
来组织我的代码。我无法在分离的班级中调用getSupportActionBar()
。如何在另一个类中获得ActionBar支持?
NewsakHome.java
package com.newsak;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Bassem on 8/26/2015.
*/
public class NewsakHome extends AppCompatActivity {
private Toolbar toolBar;
private MenuItem searchItem;
private EditText searchText;
private boolean isSearchOpened = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newsak_welcome);
toolBar = (Toolbar) findViewById(R.id.tool_bar_newsak);
// toolBar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolBar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.search_bar_id:
handleMenuSearch();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
searchItem = menu.findItem(R.id.search_bar_id);
return super.onPrepareOptionsMenu(menu);
}
protected void handleMenuSearch(){
ActionBar actionBar = getSupportActionBar();
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(isSearchOpened) // is cloases
{
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(false);
// hide the Keyboard
inputManager.hideSoftInputFromWindow(searchText.getWindowToken() ,0);
//set the closed Icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
searchItem.setIcon(ContextCompat.getDrawable(this, R.drawable.search_icon_open));
} else
{
searchItem.setIcon(getResources().getDrawable(R.drawable.search_icon_open));
}
isSearchOpened = false;
}
else
{
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setCustomView(R.layout.search_bar);
actionBar.setDisplayShowCustomEnabled(true); // set our search layout
searchText = (EditText) actionBar.getCustomView().findViewById(R.id.search_text_id);
searchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
doSearch(view.getText().toString());
return true;
}
return false;
}
});
searchText.requestFocus();
//open Keyboard
inputManager.showSoftInput(searchText, InputMethodManager.SHOW_IMPLICIT);
//set close icon
//set the closed Icon
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
searchItem.setIcon(ContextCompat.getDrawable(this, R.drawable.search_icon_closed));
} else
{
searchItem.setIcon(getResources().getDrawable(R.drawable.search_icon_closed));
}
isSearchOpened = true;
}
}
//You’d add this method too, to close the search entry with the backbutton:
public void onBackPressed() {
if(isSearchOpened)
{
handleMenuSearch();
return;
}
super.onBackPressed();
}
private void doSearch(String value) {
Toast.makeText(this, "your value is :"+value, Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:2)
您可以将NewsakHome
的引用传递到菜单功能中,如下所示:
// MenuSearchClass.java
public class MenuSearchClass() {
public static void handleMenuSearch(NewsakHome context) {
// use 'context' whenever you want to access one of NewsakHome's methods/members
ActionBar actionBar = context.getSupportActionBar();
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
...
}
}
然后,只要您调用它,就将this
传递给handleMenuSearch()
:
// NewsakHome.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.search_bar_id:
// 'this' will become 'context' in handleMenuSearch()
// handleMenuSearch() is static to make the example easier to read
MenuSearchClass.handleMenuSearch(this);
return true;
}
return super.onOptionsItemSelected(item);
}
我确实同意@Peter Brittain。你不应该只是为了使它更小或更具可读性而分裂一个类。活动类通常会处理很多功能,因此它们是项目中较大,更复杂的类的一部分是有道理的。