android

按钮 菜单 跳转

目录结构
在这里插入图片描述
manifests中
AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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=".Second">

        </activity>
    </application>

</manifest>

MainActivity

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button1=(Button)findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener(){
//Toast是Android系统提供的一种非常好的提醒方式,在程序中可以使用它将一些短小的信息通知 给用户,
// 这些信息会在一段时间后自动消失,并且不会占用任何屏幕空间,我们现在就尝试一 下如何在活动中使用Toast。
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"you click button_1",Toast.LENGTH_SHORT).show();
            }//,可以通过findViewById()	方法获取到在布局文件中定义的元素,这里我们传 入R.id.button_1	,来
            // 得到按钮的实例,这个值是刚才在first_layout.xml中通过android:id 属性指定的。findViewById()
            // 方法返回的是一个View	对象,我们需要向下转型将它转 成Button	对象。得到按钮的实例之后,我们通过调用setOnClickListener()
            // 方法为按钮 注册一个监听器
            // ,点击按钮时就会执行监听器中的onClick()	方法。因此,弹出Toast的功能当 然是要在onClick()	方法中编写了。
        });
        Button button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent	=	new	Intent(MainActivity.this,	Second.class);
                startActivity(intent);
            }

        });

    }
    public	boolean	onCreateOptionsMenu(Menu menu)	{
        getMenuInflater().inflate(R.menu.main,	menu);
        return	true;
    }
    public	boolean	onOptionsItemSelected(MenuItem item)
    {
        switch	(item.getItemId())	{//,通过调用item.getItemId()	来判断我们点击的
                case R.id.add_item:
                Toast.makeText(this,"You	clicked	Add",Toast.LENGTH_SHORT).show();
                break;
                case R.id.remove_item:
                Toast.makeText(this,"You	clicked	Remove",Toast.LENGTH_SHORT).show();
                case R.id.add_item1:
                    Toast.makeText(this,"You	clicked	Add1",Toast.LENGTH_SHORT).show();
                case R.id.remove_item1:
                    Toast.makeText(this,"hahaha",Toast.LENGTH_SHORT).show();
                break;default:				}
        return	true;
    }

}

Second

package com.example.myapplication;
import android.app.AppComponentFactory;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;


public class Second extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <TextView
        android:id="@+id/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" />

    <Button
        android:id="@+id/button_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="52dp"
        android:text="Button1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.482"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Button2"
        app:layout_constraintStart_toStartOf="@+id/button_1"
        app:layout_constraintTop_toBottomOf="@+id/button_1" />

</androidx.constraintlayout.widget.ConstraintLayout>

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        android:background="@drawable/ic_launcher_foreground" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item	android:id="@+id/add_item"	android:title="Add"/>
    <item  android:id="@+id/remove_item" android:title="Remove"/>
    <item	android:id="@+id/add_item1"	android:title="Add1"/>
    <item  android:id="@+id/remove_item1" android:title="Remove1"/>
</menu>

效果

点击BUTTON1
在这里插入图片描述
点击BUTTON2
在这里插入图片描述
点击右上角
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值