我刚刚创建了一个工具栏,有这样的图标 Sample
我希望这个黑色图标可点击 这是我的menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/searchicon"
android:icon="@drawable/search"
android:title="@string/search"
app:showAsAction="always"/>
<item android:id="@+id/listicon"
android:icon="@drawable/list_view"
android:title="@string/listview"
app:showAsAction="always"/>
<item android:id="@+id/gridicon"
android:icon="@drawable/table_grid"
android:title="@string/gridview"
app:showAsAction="always"/>
</menu>
这是我的活动,其中包含带图标的工具栏
package abtech.waiteriano.com.waitrer;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import abtech.waiteriano.com.waitrer.adapters.CustomMenuGridViewAdapter;
import abtech.waiteriano.com.waitrer.connection_class.ConnectionClass;
import abtech.waiteriano.com.waitrer.getters_and_setters.MenuItem;
public class MenuActivity extends AppCompatActivity {
private android.support.v7.widget.Toolbar toolbar;
private ArrayList<String> category ;
GridView menuGridView;
static ArrayList<MenuItem> menuGridArray = new ArrayList<MenuItem>();
CustomMenuGridViewAdapter customMenuGridViewAdapter;
ImageView listIcon, gridIcon;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
menuGridView = (GridView) findViewById(R.id.menuGridView);
customMenuGridViewAdapter = new CustomMenuGridViewAdapter(MenuActivity.this, R.layout.menu_row_grid, menuGridArray);
menuGridView.setAdapter(customMenuGridViewAdapter);
String menugridSTR = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
ResultSet rs = ConnectionClass.Ret_RS(menugridSTR);
menuGridArray.clear();
try {
while (rs.next()) {
menuGridArray.add(new MenuItem(rs.getString("Name")));
}
} catch (SQLException e) {
e.printStackTrace();
}
listIcon = (ImageView) findViewById(R.id.listicon);
gridIcon = (ImageView) findViewById(R.id.gridicon);
category = new ArrayList<String>() ;
String str = "Select ID,Code,Name,Name2 From Presets Where Active = 1 And Rest_ID_Active = 1 AND OutLet_ID_Active = 1 ORDER BY Code";
rs = ConnectionClass.Ret_RS(str);
try {
while (rs.next()) {
category.add(rs.getString("Name"));
}
} catch (SQLException e) {
e.printStackTrace();
}
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, category);
navigationSpinner.setAdapter(adapter);
toolbar.addView(navigationSpinner, 0);
navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MenuActivity.this,
"you selected: " + category.get(position),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.iconmenu, menu);
return true;
}
}
我是否应该使用OnCreate选项菜单,如果是,那我该如何使用它, 对不起,如果有任何事情不清楚是否有任何意见,请添加评论
答案 0 :(得分:3)
要处理menu
中发生的点击事件,您必须覆盖onOptionsItemSelected
的{{1}}方法,如下所示:
Activity
答案 1 :(得分:3)
覆盖方法onOptionsItemSelected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.searchicon:
//do something
return true;
case R.id.listicon:
//do something
return true;
case R.id.gridicon:
//do something
return true;
default:
return super.onOptionsItemSelected(item);
}
}
了解更多here
答案 2 :(得分:3)
只需覆盖一个方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_reset:
// your action
break;
}
return super.onOptionsItemSelected(item);
}