0

我正在写一本食谱书,我遇到了一个问题 - 当我点击我的菜单项时,它应该发送电子邮件,并预先填写了电子邮件地址和主题,没有任何反应......

任何想法为什么?

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;         
    }    
}
4

4 回答 4

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);
        }
    }

开关所做的是将“开关”中的对象与案例相匹配,匹配“开关”的案例会被执行,因此如果菜单项 id (MenuItem item; item.getItemId()) 与 android 的 ID 匹配.R.id.home 或 R.id.recipe_suggest (我假设您正在使用操作栏,因为您指的是 Android R 文件?)这就是我所知道的最好的方法 :)

于 2012-05-04T11:58:40.440 回答
0

因为你有两个 onOptionsItemSelected?好吧,一个叫做 onOptionsItemSelected* 1 * ,需要将它合并到真正的那个中。

于 2012-05-04T11:59:16.023 回答
0

您不能只创建一个名为的方法onOptionsItemSelected1(),并期望在您点击菜单条目发送邮件时 android 会调用它。

将此方法合并到原始方法onOptionsItemSelected()并将其放在正确的情况下,它应该可以工作。

于 2012-05-04T12:00:53.137 回答
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);
    }
}
于 2012-05-04T12:03:27.070 回答