Single

AIDE Android App基础教程_第十七课答案-选项菜单

第十七课,本课中您将学习主菜单使用 ,运行基本应用程序

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
}

选项菜单可以在XML代码中以菜单资源定义

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

</menu>

我们增加了第一个菜单项“About

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

	<item
        android:title="About" />
    
    
</menu>

为了显示菜单,我们在“Activity”中添加“onCreateOptionsMenu”方法,这就是创建选项菜单的方法

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO: Implement this method
		return super.onCreateOptionsMenu(menu);
	}
}

在“onCreateOptionsMenu”方法里我们利用XML菜单资源文件来显示菜单,通过“R.menu.main_menu”来访问它

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate main_menu.xml 
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}
}

现在,添加第二个菜单项“Exit

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

	<item
        android:title="About" />
    
    <item
        android:title="Exit"/>
</menu>

首先设置“About”项目的“android:id”属性为“@+id/mainMenuAbout”,分配一个ID

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

	<item
        android:title="About" 
        android:id="@+id/mainMenuAbout"/>
    
    <item
        android:title="Exit"/>
</menu>

我们已经在“Activity”中添加了“onOptionsItemSelected”方法,当菜单项被用户选中后由系统调用

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate main_menu.xml 
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// TODO: Implement this method
		return super.onOptionsItemSelected(item);
	}
}

在“onOptionsItemSelected”方法里我们检查“item.getItemId()”是否是“R.id.mainMenuAbout”,是不是我们指定的菜单项ID

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate main_menu.xml 
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.mainMenuAbout:
				Toast.makeText(this, "This is my app!!!", Toast.LENGTH_SHORT).show();
				return true;
			
		}
		return super.onOptionsItemSelected(item);
	}
}

现在给“Exit”项目分配一个ID“mainMenuExit

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

	<item
        android:title="About" 
        android:id="@+id/mainMenuAbout"/>
    
    <item
        android:title="Exit"
        android:id="@+id/mainMenuExit"/>
</menu>

在switch语句里添加case“R.id.mainMenuExit”,调用“finish()”并返回“true

package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;

public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
	{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.main);
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate main_menu.xml 
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case R.id.mainMenuAbout:
				Toast.makeText(this, "This is my app!!!", Toast.LENGTH_SHORT).show();
            case R.id.mainMenuExit:
                finish();
				return true;
			
		}
		return super.onOptionsItemSelected(item);
	}
}

欢迎加入AIDE教程网官方交流群:961607042

暂无评论

发表评论