主界面:
activity_main.xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" android:background="@mipmap/bj_1"> <!--标题栏--> <include layout="@layout/titlebar" /> <TextView android:layout_width="wrap_content" android:layout_height="220dp"/> <Button android:id="@+id/bt_add" android:layout_width="110dp" android:layout_height="60dp" android:layout_gravity="center" android:background="#1E9AC0" android:text="记一笔" android:textColor="#FFFFFF" android:textSize="18dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/bt_show" android:layout_width="110dp" android:layout_height="60dp" android:layout_gravity="center" android:background="#1E9AC0" android:text="查看明细" android:textColor="#FFFFFF" android:textSize="18dp" /> </LinearLayout>
MainActivity文件:
1 package com.example.keep; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.Button; 9 10 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ 11 private Button button1; 12 private Button button2; 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 button1=(Button)findViewById(R.id.bt_show) ; 19 button2 = (Button) findViewById(R.id.bt_add); 20 button1.setOnClickListener(this); 21 button2.setOnClickListener(this); 22 } 23 24 @Override 25 public void onClick(View v) { 26 switch (v.getId()){ 27 case R.id.bt_show://执行按钮1 28 Intent intent =new Intent(); 29 intent.setClass(getApplicationContext(), ShowActivity.class); 30 this.startActivity(intent); 31 break; 32 case R.id.bt_add: 33 Intent intent2 =new Intent(); 34 intent2.setClass(getApplicationContext(), AddActivity.class); 35 this.startActivity(intent2); 36 break; 37 38 } 39 } 40 }
效果图: