#问题描述

首先,我们有两个Java文件和与之绑定的xml文件。此处以HistoryActivity.java,activity_history.xml 和 EventDetail.java,activity_event_detail.xml为例子。我们要实现在HistoryActivity界面中添加一个按钮,并且点击跳转到EventDetail界面。

alt

为HistoryActivity界面添加按钮 在其对应的activity_history.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<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=".HistoryActivity">

    <Button
        android:id="@+id/History"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Historical Event"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
</android.support.constraint.ConstraintLayout>

我们通过android:id="@+id/History"语句讲button的id设置为History,在之后设置点击事件时使用。

为History按钮添加点击事件

在HistoryActivity.java中:

package com.example.xff.tm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.widget.Button;
import android.widget.*;

public class HistoryActivity extends AppCompatActivity {
    Button button = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        button = (Button)findViewById(R.id.History);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(HistoryActivity.this,EventDetail.class);
                startActivity(intent);
            }
        });
    }

}


通过之前定义的button的id来找到对应button,为之设置点击监听。当发生点击事件时,通过Intent进行跳转。
#在manifests->AndroidManifest.xml中添加activity(这个步骤通常是添加点击事件之后系统自动生成,可以进行检查)

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

    <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=".HistoryActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".EventDetail"></activity>
    </application>

</manifest>

EventDetail.java,activity_event_detail.xml

作为被跳转的界面,这两个文件只需要完成自己的功能即可:
EventDetail.java:

package com.example.xff.tm;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class EventDetail extends AppCompatActivity {

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

        setContentView(R.layout.activity_event_detail);
    }
}


activity_event_detail.xml:

<?xml version="1.0" encoding="utf-8"?>
<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=".EventDetail">
    
</android.support.constraint.ConstraintLayout>

Android零基础系列教程:Android基础课程

本文转自 (2条消息) Android Studio 点击按钮跳转新界面_闷闷闷闷闷小菇的博客-CSDN博客_android studio点击跳转如有侵权,请联系删除。