読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - MenuItem to Activity

 

AndroidManifest.xml
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxxx.menutoactivity" >

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".OtherActivity"
android:label="It is other activity" />

</application>

</manifest>
 
MainActivity.java
 
package com.example.liguofeng.menutoactivity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

// Menu設定
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//SubMenu subMenu = menu.addSubMenu("To other activity");

MenuItem item = menu.add("To other activity");
// Intent設定
item.setIntent(new Intent(this, OtherActivity.class));

return super.onCreateOptionsMenu(menu);
}
}
 
OtherActivity.java
 
package com.example.liguofeng.menutoactivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class OtherActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}