如何在Android Studio上创建菜单?

时间:2016-05-05 01:54:12

标签: android-studio

对于许多伟大的程序员来说,我的答案可能非常简单。我上中学;我刚开始编写java代码。我的老师为我分配了一个项目来创建一个应用程序,该应用程序使用文本字段从用户那里获取输入,并保存数据并创建新文件和加载旧文件。请问有人可以帮助我吗?

package com.example.alex_theguitarboy.phonerepov02;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText edit1 = (EditText) findViewById(R.id.editTextName);
        EditText edit = (EditText) findViewById(R.id.editTextID);
        EditText edit2 = (EditText) findViewById(R.id.editTextDescription);
    }


    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();

       switch(id)
        {
            case R.id.menu_save:

                Toast.makeText(getApplicationContext(), "Saved Successfullly", Toast.LENGTH_SHORT).show();

                break;

            case R.id.menu_load:

                Toast.makeText(getApplicationContext(), "Loaded Successfullly", Toast.LENGTH_SHORT).show();


                break;

            case R.id.menu_new:

                Toast.makeText(getApplicationContext(), "New file created", Toast.LENGTH_SHORT).show();

                break;
        }
        return super.onOptionsItemSelected(item);
    }
}




<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.alex_theguitarboy.phonerepov02.MainActivity">
    <item
        android:id="@+id/action_menu"
        android:orderInCategory="1"
        android:title="Menu"
        app:showAsAction="always">
    <menu>
        <item
            android:id="@+id/menu_save"
            android:title="Save"
            android:icon="@drawable/save_icon"/>
        <item
            android:id="@+id/menu_load"
            android:title="Load"
            android:icon="@drawable/load_icon"/>
        <item
            android:id="@+id/menu_new"
            android:title="New Game"
            android:icon="@drawable/new_game_icon"
            />
    </menu>
    </item>
</menu>

0 个答案:

没有答案