2 dingmingliang5 dingmingliang5 于 2016.01.30 17:21 提问

eclipse中MENU按键没反应

package com.example.activitytest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class FirstActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.first_layout);
Button button1 = (Button) findViewById(R.id.button_1);
button1.setOnClickListener(new OnClickListener() {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add_item:
Toast.makeText(FirstActivity.this,"You clicked Add",Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(FirstActivity.this,"You clicked Remove",Toast.LENGTH_SHORT).show();
break;
default:
}
return true;
}
@Override
public void onClick(View v) {
Toast.makeText(FirstActivity.this, "You clicked Button 1",
Toast.LENGTH_SHORT).show();
}
});
}

/**

  • @param args */ public static void main(String[] args) { // TODO Auto-generated method stub

}

}

2个回答

crazy1235
crazy1235   Ds   Rxr 2016.01.31 20:49
已采纳

我看你代码怎么把onOptionsItemSelected 写到setOnClickListener 里面了???

91program
91program   Ds   Rxr 2016.01.30 17:31

MENU按键?按键的ID,与事件的关联都检查过了?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!