多的MenuItems ActionBarSherlock? [英] ActionBarSherlock with multiple MenuItems?

查看:108
本文介绍了多的MenuItems ActionBarSherlock?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了ABS 4.0与我的应用程序之一的两名的MenuItems,但发现一个小错误:当pressing第二个菜单项,它完全一样的第一个...

我试过只是一切我能想到的,但它不工作。我已经改变onOptionItemSelected,因为我认为这是我需要编辑的方法。

编辑:

我一直期待在@奥利的建议,但既不LogCat中也没有调试正显示出奇怪的事情。也许这是在code,或ABS声明的其他部分?这里就是整个code,如果你可以看看通过它,那将是伟大的!

在code为整个活动,因为它可能在其他地方?

 包bas.sie.Antonius;进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.TextView;进口com.actionbarsherlock.app.ActionBar;
进口com.actionbarsherlock.app.SherlockActivity;
进口com.actionbarsherlock.view.Menu;
进口com.actionbarsherlock.view.MenuItem;公共类TeacherInfo扩展SherlockActivity {    串URLhome;
    字符串信息;
    串TeacherAb;
    TextView的mTxtvInfo;
    按钮mBtnTeacherStSchedule;
    按钮mBtnTeacherDaySchedule;
    私人静态字符串mainUrl =htt​​p://www.carmelcollegegouda.nl/site_ant/;
    私人静态字符串endUrl =热媒;
    私有静态的String [] = myUrls {雄鸡/ dagroosters / Doc_V1_
            公鸡/ standaardroosters / Doc1_};    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.contactinfo);
        的setTitle(在德泽leraar);        动作条动作条= getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(真);        mTxtvInfo =(的TextView)findViewById(R.id.TxtvTeacher);        意向startingIntent = getIntent();
        信息= startingIntent.getStringExtra(接触);
        mTxtvInfo.setText(信息);        意向startingIntent1 = getIntent();
        TeacherAb = startingIntent1.getStringExtra(缩写);        mBtnTeacherDaySchedule =(按钮)findViewById(R.id.btnTeacherDaySchedule);
        mBtnTeacherStSchedule =(按钮)findViewById(R.id.btnTeacherStSchedule);        mBtnTeacherDaySchedule.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看视图){                URLhome = makeUrl(0);                意图I =新意图(TeacherInfo.this,MyWebView.class);
                i.putExtra(家,URLhome);
                startActivityForResult(I,0);
            }
        });        mBtnTeacherStSchedule.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(查看视图){                URLhome = makeUrl(1);                意图I =新意图(TeacherInfo.this,MyWebView.class);
                i.putExtra(家,URLhome);
                startActivityForResult(I,0);
            }
        });    }    私人字符串makeUrl(INT指数){
        字符串s = mainUrl + myUrls [指数] + TeacherAb + endUrl;
        返回S;
    } // makeurl    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        menu.add(Instellingen)
                .setIcon(R.drawable.ic_settings)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM
                                | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        menu.add(在德的应用程序)
                .setIcon(R.drawable.ic_about)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM
                                | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        返回super.onCreateOptionsMenu(菜单);
    }
    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            意向意图=新意图(这一点,AntoniusActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(意向);
            返回true;
        案例R.id.settings:
            意图I =新意图(这一点,About.class);
            startActivity(ⅰ);
            返回true;
        案例R.id.about:
            关于意向=新意图(这一点,About.class);
            startActivity(约);
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
        }
    }}

我想这个问题是在菜单项的声明,但我看不出有什么问题...

你能帮我看看我menu.xml文件?在这里发布:

 <?XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目机器人:ID =@ + ID /设置
          机器人:图标=@绘制/ ic_settings
          机器人:标题=Instellingen>< /项目>
    <项目机器人:ID =@ + ID /约
          机器人:图标=@绘制/ ic_about
          机器人:标题=在德的应用程序>< /项目>
&所述; /菜单>


解决方案

创建菜单是这样的:

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    MenuInflater吹气= getMenuInflater();
    inflater.inflate(R.menu.menu,菜单);
    返回super.onCreateOptionsMenu(菜单);
}

然后使用switch语句来处理选择:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例android.R.id.home:
            //做的东西
            返回true;
        案例R.id.menu_item_2:
            //做的东西
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
    }
}

修改:最后,你应该为每个项目做不同的事情,如果你改变了意向目标活动到另一个,它会做你所期望的:

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
    案例android.R.id.home:
        // ... 东东 ...
    案例R.id.settings://设置项
        意图I =新意图(这一点,About.class); //启动About.java活动,但项目写着设置
        // TODO:变化关于设置?
        I =新意图(这一点,Settings.class);
        startActivity(ⅰ);
        返回true;    案例R.id.about://关于项目
        关于意向=新意图(这一点,About.class); //开始About.java Activty
        startActivity(约);
        返回true;    默认:
        返回super.onOptionsItemSelected(项目);
    }
}

I've been using ABS 4.0 with two MenuItems in one of my apps, but have discovered a little error: When pressing the second MenuItem, it does exactly the same as the first one...

I've tried just about everything I can think of, but it isn't working. I've altered onOptionItemSelected, as I thought that was the method I need to edit.

EDIT:

I've been looking at @Ollie's suggestions, but neither LogCat nor Debug is showing weird things. Maybe it's in some other part of the code, or a declaration for ABS? Here's the entire code, if you could look through it, that would be great!

The code for the whole Activity, as it's maybe in some other place?

package bas.sie.Antonius;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class TeacherInfo extends SherlockActivity {

    String URLhome;
    String Info;
    String TeacherAb;
    TextView mTxtvInfo;
    Button mBtnTeacherStSchedule;
    Button mBtnTeacherDaySchedule;
    private static String mainUrl = "http://www.carmelcollegegouda.nl/site_ant/";
    private static String endUrl = ".htm";
    private static String[] myUrls = { "roosters/dagroosters/Doc_V1_",
            "roosters/standaardroosters/Doc1_" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactinfo);
        setTitle("Over deze leraar");

        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        mTxtvInfo = (TextView) findViewById(R.id.TxtvTeacher);

        Intent startingIntent = getIntent();
        Info = startingIntent.getStringExtra("contact");
        mTxtvInfo.setText(Info);

        Intent startingIntent1 = getIntent();
        TeacherAb = startingIntent1.getStringExtra("abbrev");

        mBtnTeacherDaySchedule = (Button) findViewById(R.id.btnTeacherDaySchedule);
        mBtnTeacherStSchedule = (Button) findViewById(R.id.btnTeacherStSchedule);

        mBtnTeacherDaySchedule.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                URLhome = makeUrl(0);

                Intent i = new Intent(TeacherInfo.this, MyWebView.class);
                i.putExtra("home", URLhome);
                startActivityForResult(i, 0);
            }
        });

        mBtnTeacherStSchedule.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                URLhome = makeUrl(1);

                Intent i = new Intent(TeacherInfo.this, MyWebView.class);
                i.putExtra("home", URLhome);
                startActivityForResult(i, 0);
            }
        });

    }

    private String makeUrl(int index) {
        String s = mainUrl + myUrls[index] + TeacherAb + endUrl;
        return s;
    }// makeurl

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Instellingen")
                .setIcon(R.drawable.ic_settings)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM
                                | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        menu.add("Over de app")
                .setIcon(R.drawable.ic_about)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_IF_ROOM
                                | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            Intent intent = new Intent(this, AntoniusActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        case R.id.settings:
            Intent i = new Intent(this, About.class);
            startActivity(i);
            return true;
        case R.id.about:
            Intent about = new Intent(this, About.class);
            startActivity(about);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

}

I'm thinking that the problem is in the declaration of the menu items, but I don't see any problem there...

Could you take a look at my menu.xml? Posted here:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/settings" 
          android:icon="@drawable/ic_settings" 
          android:title="Instellingen"></item>
    <item android:id="@+id/about" 
          android:icon="@drawable/ic_about" 
          android:title="Over de app"></item>
</menu>

解决方案

Create the menu like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

Then use a switch statement to handle selections:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // Do stuff
            return true;
        case R.id.menu_item_2:
            // Do stuff
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

EDIT: Finally, you should do different things for each item, if you change the Intent target Activity to another, it'll do what you expect:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // ... Stuff ...
    case R.id.settings: // Settings item
        Intent i = new Intent(this, About.class); // Start About.java Activity, but item says "settings"
        // TODO: Change About to Settings?
        i = new Intent(this, Settings.class);
        startActivity(i);
        return true;

    case R.id.about: // About item
        Intent about = new Intent(this, About.class); // Start About.java Activty
        startActivity(about);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

这篇关于多的MenuItems ActionBarSherlock?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆