问题形容
首先,咱们有两个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点击跳转如有侵权,请分割删除。
发表回复