处理菜单项点击事件 - Android
Posted
技术标签:
【中文标题】处理菜单项点击事件 - Android【英文标题】:Handling a Menu Item Click Event - Android 【发布时间】:2011-11-20 18:06:07 【问题描述】:我想创建一个在单击菜单项后启动新活动的意图,但我不确定如何执行此操作。我一直在阅读 android 文档,但我的实现不正确..一些正确方向的指导会有所帮助。我在下面列出了我的代码并注释掉了我的问题区域,我认为我调用了错误的方法。
package com.jbsoft.SimpleFlashlight;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.view.MenuItem.OnMenuItemClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SimpleFlashLightActivity extends Activity
Button GreenButton; // Declare instances of buttons to use later
Button BlueButton;
private static final int OK_MENU_ITEM = Menu.FIRST;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
BlueButton = (Button) findViewById(R.id.bluebutton);
BlueButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
//Display msg when user clicks Blue Button
showColorChangeMsg();
// Switch Activities on click
Intent blueintent = new Intent(SimpleFlashLightActivity.this,
BlueFlashLightActivity.class);
startActivity(blueintent);
);
//Install listener for second button
GreenButton = (Button) findViewById(R.id.greenbutton);
GreenButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
// Display msg when user clicks Green Button
showColorChangeMsg();
Intent greenintent = new Intent(SimpleFlashLightActivity.this,
GreenFlashLightActivty.class);
startActivity(greenintent);
);
;
/**************************************************************************************/
// Method Declarations // THIS IS WHERE I'M HAVING A PROBLEM
MenuItem AddColorButton = (MenuItem)findViewById(R.id.menu_insert);
boolean onOptionsItemSelected(AddColorButton)
Intent intent = new Intent(SimpleFlashLightActivity.this,
BlueFlashLightActivity.class);
startActivity(intent);
return true;
;
;
/****************************************************************************************/
private void showColorChangeMsg()
Toast msgtoast = Toast.makeText(this.getBaseContext(), "SWITCH COLOR!",
Toast.LENGTH_LONG);
msgtoast.show();
private void showMsg(String msg)
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.show();
public boolean onCreateOptionsMenu(Menu menu)
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.list_menu, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case OK_MENU_ITEM:
showMsg("OK");
break;
return super.onOptionsItemSelected(item);
【问题讨论】:
【参考方案1】:创建菜单的简单代码。
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
菜单项选择的简单代码
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle item selection
switch (item.getItemId())
case R.id.new_game:
newGame();
return true;
case R.id.help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
您可以从下面的链接中找到更多详细信息..
Menus
Menu resource
【讨论】:
如果我将 android:onClick 属性放在 XML 中,在这种情况下是否不起作用? (这里是非常初学者的 Android 程序员) @FateNuller onClick inside XML for options 菜单不起作用,它适用于布局。您应该处理 onOptionsItemSelected 内的操作栏选项菜单点击。 需要常量表达式。资源 ID 不能在 Android 库模块的 switch 语句中使用 less... 验证在 Android 库模块的 switch 语句中使用资源 ID。资源 ID 在库项目中不是最终的,因为 SDK 工具 r14,这意味着库代码不能将这些 ID 视为常量。【参考方案2】:添加以下代码
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case R.id.new_item:
Intent i = new Intent(this,SecondActivity.class);
this.startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
【讨论】:
【参考方案3】:菜单项文件看起来像
res/menu/menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/settings"
android:title="Setting"
app:showAsAction="never" />
<item
android:id="@+id/my_activity"
android:title="My Activity"
app:showAsAction="always"
android:icon="@android:drawable/btn_radio"/>
</menu>
Java 代码看起来像
src/MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu)
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (id == R.id.my_activity)
Intent intent1 = new Intent(this,MyActivity.class);
this.startActivity(intent1);
return true;
if (id == R.id.settings)
Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show();
return true;
return super.onOptionsItemSelected(item);
并将以下代码添加到您的 AndroidManifest.xml 文件中
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
</activity>
希望对你有帮助。
【讨论】:
【参考方案4】:除了您的问题中显示的选项外,还可以从菜单中直接在您的 xml 文件中执行操作,例如:
<item
android:id="@+id/OK_MENU_ITEM"
android:onClick="showMsgDirectMenuXml" />
而对于你的Java(Activity)文件,你需要实现一个带有MenuItem类型的单个参数的公共方法,例如:
private void showMsgDirectMenuXml(MenuItem item)
Toast toast = Toast.makeText(this, "OK", Toast.LENGTH_LONG);
toast.show();
注意:此方法的行为类似于 onOptionsItemSelected(MenuItem 项)
【讨论】:
【参考方案5】:这段代码对我有用
@Override
public boolean onOptionsItemSelected(MenuItem item)
int id = item.getItemId();
if (id == R.id.action_settings)
// add your action here that you want
return true;
else if (id==R.id.login)
// add your action here that you want
return super.onOptionsItemSelected(item);
【讨论】:
【参考方案6】:将您的onOptionsItemSelected
替换为:
@Override
public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case OK_MENU_ITEM:
startActivity(new Intent(DashboardActivity.this, SettingActivity.class));
break;
// You can handle other cases Here.
default:
super.onOptionsItemSelected(item);
在这里,我想从DashboardActivity
导航到SettingActivity
。
【讨论】:
【参考方案7】:这就是它在 Kotlin 中的样子
main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
<item
android:id="@+id/action_logout"
android:orderInCategory="101"
android:title="@string/sign_out"
app:showAsAction="never" />
然后在MainActivity中
override fun onCreateOptionsMenu(menu: Menu): Boolean
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu)
return true
这是 onOptionsItemSelected 函数
override fun onOptionsItemSelected(item: MenuItem): Boolean
return when(item.itemId)
R.id.action_settings ->
true
R.id.action_logout ->
signOut()
true
else -> return super.onOptionsItemSelected(item)
开始新的活动
private fun signOut()
MySharedPreferences.clearToken()
startSplashScreenActivity()
private fun startSplashScreenActivity()
val intent = Intent(GrepToDo.applicationContext(), SplashScreenActivity::class.java)
startActivity(intent)
finish()
【讨论】:
以上是关于处理菜单项点击事件 - Android的主要内容,如果未能解决你的问题,请参考以下文章