如何找出在菜单知道它的十进制值项的字符串ID?它的、字符串、出在、菜单

2023-09-13 23:38:10 作者:称王

我使用Android的支持-V7-appcompat。

I am using android-support-v7-appcompat.

在我要在动作条显示后退按钮的活动。 我做的:

In an activity I want to show in the actionbar the back button. I do:

    public class News extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_news_screen);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
       }
}

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        System.out.println(item.getItemId()); // 16908332
        System.out.println(R.id.home); // 2131034132
        System.out.println(R.id.homeAsUp); // 2131034117
        switch(item.getItemId())
        {
            case R.id.home:
                onBackPressed();
                break;
            case R.id.homeAsUp:
                onBackPressed();
                break;              
            case 16908332:
                onBackPressed(); // it's works
                break;              
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

如果我用数字滤波器由ID的作品,但我认为ID由R产生的,因此可以改变的,因此使用R.id. 。你知道吗?

If I use numerical filter by id works, but I think that ID is generated by R and therefore can change, is therefore used R.id. . Any idea?

推荐答案

在动作条家庭/回图标ID为 android.R.id.home 。 你可以查找ID。

The home/back icon in the actionbar has the id android.R.id.home. You can look for that id.

在android.R的值。*永远不会改变,并且静态链接。

The values in android.R.* will never change and are linked statically.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.home:
            onBackPressed();
            break;
        case R.id.homeAsUp:
            onBackPressed();
            break;              
        case android.R.id.home:
            onBackPressed();
            break;              
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}