我正在写一本食谱书,我遇到了一个问题 - 当我点击我的菜单项时,应该发送电子邮件,电子邮件地址和预先填写的主题,没有任何反应......
任何想法为什么???
public class recipedisplayscreen extends Activity {
TextView EmailAddress;
TextView EmailSubject;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recipedisplayscreen);
TextView MethodDisplay = (TextView) findViewById(R.id.textView3);
TextView IngredientsDisplay = (TextView) findViewById(R.id.textView5);
Intent i = getIntent();
String Ingredients = i.getStringExtra("textView1");
String Method = i.getStringExtra("textView2");
Log.e("recipedisplayscreen", Ingredients + "." + Method);
MethodDisplay.setText(Method);
IngredientsDisplay.setText(Ingredients);
EmailAddress=(TextView) findViewById(R.id.textView2);
EmailSubject=(TextView) findViewById(R.id.textView4);
ActionBar actionBar = getActionBar();
setTitle(R.string.title);
actionBar.setDisplayHomeAsUpEnabled(true);
setTitle(Method);}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public boolean onOptionsItemSelected1(MenuItem recipe_suggest) {
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ EmailAddress.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.recipe_menu1, menu);
return true;
}
}
答案 0 :(得分:4)
我相信这就是你想要的:D删除 onOptionsItemSelected1 并用此替换 onOptionsItemSelected ,假设recipe_suggest是菜单项的id?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case R.id.recipe_suggest:
//Other menu item I believe
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ EmailAddress.getText().toString()});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EmailSubject.getText());
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
开关的作用是将“switch”中的对象与case匹配,匹配“switch”的情况得到执行,所以如果菜单项id(MenuItem item; item.getItemId())匹配ID是android.R.id.home还是R.id.recipe_suggest(我假设您正在使用操作栏,因为您指的是Android R文件?)这就是我应该如何做到最好的知识:)
答案 1 :(得分:0)
因为你有两个onOptionsItemSelected?好吧,一个名为onOptionsItemSelected * 1 *,需要将其合并到真实的一个。
答案 2 :(得分:0)
你不能只创建一个名为onOptionsItemSelected1()
的方法,并希望当你点击菜单条目发送邮件时,android会调用它。
将此方法合并到原始onOptionsItemSelected()
并将其放在正确的情况下,而不是它应该工作。
答案 3 :(得分:0)
为什么你有一个名为onOptionsItemSelected * 1 *的单独方法?
对行动项目作出反应的所有代码应位于 onOptionsItemSelected :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// App icon in action bar clicked; go home
Intent intent = new Intent(this, MainScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
case android.R.id.email_action_item:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
(...)
default:
return super.onOptionsItemSelected(item);
}
}