【android编程】 第六讲-Android菜单实践题

【android编程】 第六讲-Android菜单实践题

笔记链接:https://blog.csdn.net/xbean1028/article/details/104797732

文章目录

  • 【android编程】 第六讲-Android菜单实践题
    • 1. 带子菜单的选项菜单练习
      • 1. 效果展示
      • 2. menu\menu_main.xml
      • 2. MainActivity.java
    • 2. 上下文菜单(长按)练习
      • 1. 效果展示
      • 2. activity_main.xml
      • 3. MainActivity.java

1. 带子菜单的选项菜单练习

1. 效果展示

【android编程】 第六讲-Android菜单实践题_第1张图片

2. menu\menu_main.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:checkableBehavior="all">

    <item
        android:id="@+id/item1"
        android:alphabeticShortcut="e"
        android:title="File">
        <menu>
            <group android:id="@+id/file">
                <item android:id="@+id/open" android:title="Open">item>
                <item android:id="@+id/close" android:title="Close">item>
            group>
        menu>
    item>
    <group android:checkableBehavior="all">
        <item
            android:id="@+id/item2"
            android:title="文本文件"
            android:alphabeticShortcut="r">item>
        <item
            android:id="@+id/item3"
            android:alphabeticShortcut="g"
            android:title="二进制文件">item>
    group>

menu>

2. MainActivity.java

package com.example.bean.myapplicationv0;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.item1:
                Toast.makeText(this,"File", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item2:
                Toast.makeText(this,"文本文件", Toast.LENGTH_SHORT).show();
                break;
            case R.id.item3:
                Toast.makeText(this,"二进制文件", Toast.LENGTH_SHORT).show();
                break;
            case R.id.open:
                Toast.makeText(this,"Open", Toast.LENGTH_SHORT).show();
                break;
            case R.id.close:
                Toast.makeText(this,"Close", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

2. 上下文菜单(长按)练习

1. 效果展示

【android编程】 第六讲-Android菜单实践题_第2张图片

2. activity_main.xml


<android.support.constraint.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.bean.myapplicationv2.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/textView"
        app:layout_constraintVertical_bias="0.658" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/y20"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />

android.support.constraint.ConstraintLayout>

3. MainActivity.java

package com.example.bean.myapplicationv2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView image = (ImageView)findViewById(R.id.imageView);
        registerForContextMenu(image);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        Toast.makeText(this,item.getTitle(),Toast.LENGTH_SHORT).show();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("图中的飞机是");
        menu.add(1,1,1,"轰战机");
        menu.add(1,2,1,"运输机");

    }
}

你可能感兴趣的:(Android)