Jad*_*eld 108 android event-handling menuitem options-menu onitemclicklistener
我想创建一个在单击菜单项后启动新活动的intent,但我不知道如何执行此操作.我一直在阅读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);
}
}
Run Code Online (Sandbox Code Playgroud)
Nir*_*tel 267
创建菜单的简单代码..
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.game_menu, menu);
return true;
}
Run Code Online (Sandbox Code Playgroud)
选择菜单的简单代码
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
更多细节请参阅以下链接..
小智 9
添加以下代码
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
除了您的问题中显示的选项外,还可以从菜单中直接在 xml 文件中实施该操作,例如:
<item
android:id="@+id/OK_MENU_ITEM"
android:onClick="showMsgDirectMenuXml" />
Run Code Online (Sandbox Code Playgroud)
而对于您的 Java (Activity) 文件,您需要使用 MenuItem 类型的单个参数实现一个公共方法,例如:
private void showMsgDirectMenuXml(MenuItem item) {
Toast toast = Toast.makeText(this, "OK", Toast.LENGTH_LONG);
toast.show();
}
Run Code Online (Sandbox Code Playgroud)
注意:此方法的行为类似于 onOptionsItemSelected(MenuItem 项)
菜单项文件看起来像
RES /菜单/ 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>
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
并将以下代码添加到AndroidManifest.xml文件中
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
</activity>
Run Code Online (Sandbox Code Playgroud)
我希望它会对你有所帮助.
归档时间: |
|
查看次数: |
240512 次 |
最近记录: |