Android studio 點選按鈕 (跳轉介面)
阿新 • • 發佈:2021-12-10
問題描述
首先,我們有兩個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>
本文轉自(2條訊息) Android Studio 點選按鈕跳轉新介面_悶悶悶悶悶小菇的部落格-CSDN部落格_android studio點選跳轉
如有侵權,請聯絡刪除。
Android零基礎系列教程:Android基礎課程