Java 如何在mainactivity中使用onClickListener和onMenuSelectListener?
你好,我尝试在我的mainactivity类中使用onclicklistener和onmenuselectlistener,程序运行,但是选择弹出菜单操作不起作用,他们不会设置文本想要它想要任何人有任何想法吗?我知道我没有实现onMenuSelectListener,如果有其他方法可以实现的话,这可能是我的问题 这是我的密码:Java 如何在mainactivity中使用onClickListener和onMenuSelectListener?,java,android,onclicklistener,popupmenu,optionmenu,Java,Android,Onclicklistener,Popupmenu,Optionmenu,你好,我尝试在我的mainactivity类中使用onclicklistener和onmenuselectlistener,程序运行,但是选择弹出菜单操作不起作用,他们不会设置文本想要它想要任何人有任何想法吗?我知道我没有实现onMenuSelectListener,如果有其他方法可以实现的话,这可能是我的问题 这是我的密码: public class MainActivity extends Activity implements OnClickListener { // init varia
public class MainActivity extends Activity implements OnClickListener {
// init variables
Handler uiHandler;
EditText cl;
TextView info;
Button enter;
Button line;
Button arc;
DrawingUtils callDU = new DrawingUtils();
DrawingTools callDT = new DrawingTools();
EditTools callET = new EditTools();
Conversion callConversion = new Conversion();
GLSurfaceView mGLSurface;
String Tag = "Debug";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mGLSurface = new GLSurfaceView(this);
mGLSurface.setRenderer(new BasicRenderer());
setContentView(R.layout.canvas);
FrameLayout v = (FrameLayout) findViewById(R.id.canvas);
v.addView(mGLSurface);
// init views and buttons
info = (TextView) findViewById(R.id.info);
enter = (Button) findViewById(R.id.enter);
line = (Button) findViewById(R.id.line);
arc = (Button) findViewById(R.id.arc);
cl = (EditText) findViewById(R.id.cl);
/*
* Handler for Main Thread uiHandler = new Handler() { public void
* handleMessage(Message msg) { switch (msg.what) {
*
* } Bundle bundle = msg.getData(); String string1 =
* bundle.getString("P1Key"); String string2 =
* bundle.getString("P2Key"); info.setText(string1);
* info.setText(string2); } };
*/
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.enter:
break;
case R.id.line:
break;
case R.id.arc:
break;
}
};
public void CreatePopupMenu(View v) {
PopupMenu mypopupmenu = new PopupMenu(this, v);
MenuInflater inflater = mypopupmenu.getMenuInflater();
inflater.inflate(R.menu.filemenu, mypopupmenu.getMenu());
mypopupmenu.show();
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.newCanas:
info.setText("New");
Log.d(Tag, "New was clicked");
break;
case R.id.open:
break;
case R.id.save:
break;
}
return super.onMenuItemSelected(featureId, item);
}
}看起来您并没有将onClickListeners附加到任何东西上。这意味着你说的是“每当以我为目标触发onClick事件时,执行此操作”。但你永远不会把自己当成目标。尝试将以下代码添加到onCreate中
arc.setOnClickListener(this);
line.setOnClickListener(this);
enter.setOnClickListener(this);
出现的
弹出菜单
也会发生同样的情况。尝试添加mypopupmenu.addOnMenuItemSelectListener(此)
在为菜单展开布局后立即添加。乔纳森是对的。您应该添加.setOnClickListener(此)代码>添加到创建按钮后添加的每个按钮
但是,对于菜单项,您必须执行以下操作:
1) 使用菜单上的项目创建布局,并将其存储在res/menu/
目录中
例如:
main.xml
3) 重写方法onOptionsItemSelected()
,以像处理actionListeners一样使用开关执行任何操作
4) 此外,您还可以重写方法onPrepareOptions菜单()
,该方法与上一个方法相同,但每次打开菜单时都会调用该方法
祝你好运菜单项呢?这取决于OP的菜单项是在选项菜单中还是只是一个普通的旧菜单。鉴于OP没有调用onCreateOptions菜单
,我认为OP没有使用选项菜单。如果他或她是,那么你是对的。单击按钮文件时,弹出菜单。除此之外,我不确定选项菜单和普通的旧菜单之间的区别。那么,在这种情况下,你应该遵循菜单。它提供各种不同的菜单(上下文、弹出窗口等)
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}