关于android:Android-studio-点击按钮-跳转界面

4次阅读

共计 3229 个字符,预计需要花费 9 分钟才能阅读完成。

问题形容

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

为 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 点击跳转如有侵权,请分割删除。

正文完
 0