• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Android-studio-点击按钮-跳转界面

android 搞代码 3年前 (2022-03-01) 32次浏览 已收录 0个评论

问题形容

首先,咱们有两个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点击跳转如有侵权,请分割删除。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Android-studio-点击按钮-跳转界面

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址